leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
题目例如以下:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
- What constitutes a word?
A sequence of non-space characters constitutes a word. - Could the input string contain leading or trailing spaces?
Yes. However, your reversed string should not contain leading or trailing spaces. - How about multiple spaces between two words?
Reduce them to a single space in the reversed string.
class Solution {
public:
void reverseWords(string &s)
{
if(s.empty())
return;
int count = 0;
int index = 0, indexTemp = 0, begin = 0, end = s.length()-1;
while(s[begin] == ' ' && begin<s.length()-1)
begin++;
while(s[end] == ' ' && end>0)
end--;
if(end == 0 && s[end] == ' ')
{
s = "";
return;
}
else if(end == 0)
{
s = s.substr(0,1);
return;
}
index = begin;
while(index <= end)
{
if(s[index] == ' ')
{
count++;
while(s[index] == ' ')
index++;
}
count++;
index++;
}
string temp(count,'\0');
index = end;
indexTemp = 0;
while(index >= begin)
{
if(s[index] == ' ')
{
temp[indexTemp] = s[index];
while(s[index] == ' ')
index--;
indexTemp++;
}
temp[indexTemp] = s[index];
indexTemp++;
index--;
}
indexTemp = 0;
begin = -1;
end = -1;
while(indexTemp < count)
{
if(temp[indexTemp] != ' ' && begin == -1)
{
begin = 0;
}
else if(indexTemp-1 >= 0 && temp[indexTemp-1] == ' '&&temp[indexTemp] != ' ')
{
begin = indexTemp;
}
else if((indexTemp+1 < count && temp[indexTemp+1] == ' ' && temp[indexTemp] != ' ') || ((indexTemp == count-1)&&temp[indexTemp] != ' '))
{
end = indexTemp;
reverse(temp, begin, end); }
indexTemp++;
}
s = temp;
} void reverse(string &s, int begin, int end)
{
while(begin < end)
{
char temp = s[begin];
s[begin] = s[end];
s[end] = temp;
begin++;
end--;
}
}
};
leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)的更多相关文章
- [LeetCode] Reverse Vowels of a String 翻转字符串中的元音字母
Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- 【LeetCode】Reverse Words in a String 反转字符串中的单词
一年没有管理博客园了,说来实在惭愧.. 最近开始刷LeetCode,之前没刷过,说来也实在惭愧... 刚开始按 AC Rates 从简单到难刷,觉得略无聊,就决定按 Add Date 刷,以后也可能看 ...
- [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串
Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...
- [LintCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 345 Reverse Vowels of a String 反转字符串中的元音字母
编写一个函数,以字符串作为输入,反转该字符串中的元音字母.示例 1:给定 s = "hello", 返回 "holle".示例 2:给定 s = "l ...
- LeetCode Reverse Words in a String 将串中的字翻转
class Solution { public: void reverseWords(string &s) { string end="",tem="" ...
- 345. Reverse Vowels of a String翻转字符串中的元音字母
[抄题]: Write a function that takes a string as input and reverse only the vowels of a string. Example ...
随机推荐
- ECMALL功能拓扑图以及模式分析
ECMALL VS 常规的B2C产品(以ECSHOP做对比)的区别: 1.支持多用户在同一个域名下开店. 2.开店的卖家各自结算,直接收钱.平台只是提供了一个类似传统行业的摊位.平台不过手金钱 3 ...
- stm32单片机的封装
接着去查看VREF...
- angular 设置全局常量
一:在项目核心文件core.module.ts中设置全局静态常量 解释:相当于自动注入到inject中. providers:[ { provide:'BASE_CONFIG', useValue:' ...
- echarts3.0 仪表盘实例更改完成占用率实例
需要完成的项目效果 官方实例效果 基本思路: 首先引入jquery和echarts3.0库. 需要两个仪表盘,一个仪表盘是纯色灰色,在底部.startAngle 和endAngle永远是最大值,默认为 ...
- (转)xshell无法在vim中复制黏贴
ssh xshell 连接在vim中无法用 ctrl+insert 复制黏贴 修改.vimrc set mouse=c vi的三种模式:命令模式,插入模式,可视模式.鼠标可以启动于各种模式中: The ...
- Maven学习总结(16)——深入理解maven生命周期和插件
在项目里用了快一年的maven了,最近突然发现maven项目在eclipse中build时非常慢,因为经常用clean install命令来build项目,也没有管那么多,但最近实在受不了乌龟一样的b ...
- Android OnGestureListener用法 识别用户手势 左右滑动
Android可以识别用户的手势(即用户用手指滑动的方向),通过用户不同的手势,从而做出不同的处理 需要使用OnGestureListener 比如说看电子书的时候翻页,或者要滑动一些其他内容 直接上 ...
- libjpeg用法
libjpeg是一个完全用C语言编写的库,包含了被广泛使用的JPEG解码.JPEG编码和其他的JPEG功能的实现.这个库由独立JPEG工作组维护.最新版本号是6b,于1998年发布.可以参考维基百科关 ...
- IOS手势事件
一, iPhone中处理触摸事件的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式 - (void)touchesBegan:(NSSet *)touches withEve ...
- 解读Java中BigDecimal.ZERO.compareTo()的返回值含义
Java compareTo() 用法 例如: public static void main(String[] args) { BigDecimal bnum1, bnum2; bnum1 ...