当前位置:网站首页>2000. 反转单词前缀
2000. 反转单词前缀
2022-07-23 00:46:00 【Mr Gao】
2000. 反转单词前缀
给你一个下标从 0 开始的字符串 word 和一个字符 ch 。找出 ch 第一次出现的下标 i ,反转 word 中从下标 0 开始、直到下标 i 结束(含下标 i )的那段字符。如果 word 中不存在字符 ch ,则无需进行任何操作。
例如,如果 word = "abcdefd" 且 ch = "d" ,那么你应该 反转 从下标 0 开始、直到下标 3 结束(含下标 3 )。结果字符串将会是 "dcbaefd" 。
返回 结果字符串 。
示例 1:
输入:word = “abcdefd”, ch = “d”
输出:“dcbaefd”
解释:“d” 第一次出现在下标 3 。
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 “dcbaefd” 。
示例 2:
输入:word = “xyxzxe”, ch = “z”
输出:“zxyxxe”
解释:“z” 第一次也是唯一一次出现是在下标 3 。
反转从下标 0 到下标 3(含下标 3)的这段字符,结果字符串是 “zxyxxe” 。
示例 3:
输入:word = “abcd”, ch = “z”
输出:“abcd”
解释:“z” 不存在于 word 中。
无需执行反转操作,结果字符串是 “abcd” 。
解题代码如下:
int find_inde(char *s,char ch){
int i=0;
while(s[i]!='\0'){
if(s[i]==ch){
return i;
}
i++;
}
return -1;
}
void reverse(char *s,int index){
int i;
for(i=0;i<index/2+1;i++){
char t=s[index-i];
s[index-i]=s[i];
s[i]=t;;
}
}
char * reversePrefix(char * word, char ch){
int index=find_inde(word,ch);
if(index!=-1){
reverse(word,index);
}
return word;
}
边栏推荐
- 为什么使用Well-Architected Framework?
- LiveQing直播RTMP点播视频流媒体平台如何携带登录接口返回的sid和token接口调用鉴权streamToken视频流鉴权
- SIP账号的作用-告诉你什么是SIP线路
- [数组]NC77 调整数组顺序使奇数位于偶数前面(一)-中等
- Jmeter---Jmeter安装教程
- openresty lua-resty-balancer动态负载均衡
- 讲一讲HART协议
- OSI七层模型有哪七层?每一层分别有啥作用,这篇文章讲的明明白白!
- TCP数据传输与性能
- DALSA smart camera boa spot communicates with Siemens s7-1200 PROFINET
猜你喜欢

Anti attack based on conjugate gradient method

PMP一手资料、一手资讯获取

Internet Download Manager简直就是下载器中的大杀器

【华为联机对战服务】客户端退出重连或中途进入游戏,新玩家如何补帧?

In depth explanation of CAS is necessary for interview practice

50道经典计算机网络面试题,你答得上几个?(三)

50道经典计算机网络面试题,你答得上几个?(二)

Unity中实现判断Missing还是Null

Sort out 55 MySQL interview questions (including answers)

UGUI源码解析——StencilMaterial
随机推荐
工作中遇到一个bug的解决过程
canal实现Mysql数据同步
OSPF的路由控制
超全PMP备考文档汇总
Is it safe for CICC fortune to open an account? How to buy stocks
Geely Xingrui: from product technology empowerment to cultural confidence
Implementation of OA office system based on JSP
解析steam与创客教育课堂的统筹规划
差分数组操作的一些性质
博途PLC信号处理系列之限幅消抖滤波
MGRE 网络的构建
User defined type details: structure, enumeration, union
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code
SIP账号的作用-告诉你什么是SIP线路
Is it safe to open an account online? How about Galaxy Securities
The concept and method of white box test
How many of the 50 classic computer network interview questions can you answer? (II)
SQL Server 数据库设计--SELECT语句
Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)
【并发编程】第二章:从核心源码深入ReentrantLock锁