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 ...
随机推荐
- [D3] Add image to the node
We can create node with 'g' container, then append 'image' to the nodes. // Create container for the ...
- iOS 中使用 XIB 自定义cell的两种方法以及编译出现常见 的错误 (xcode6.0之后)
一. 注册cell 1.创建自定义cell并勾选 xib :(勾选xib就会自动生成与cell文件关联的xib) 2.在 tableViewController里注册自定义Cell (或者遵守tabl ...
- LeetCode Algorithm 04_Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- Hamming correct
从数的最左边开始,并标记为1 将2的平方的位置留出来,做为校验位例如,8位2进制数10011010===>_ _ 1 _ 0 0 1 _ 1 0 1 0 位置1用来校验最右边的位位1的位置1 3 ...
- Maven使用yuicompressor-maven-plugin打包压缩css、js文件
最近项目想使用在maven打包的时间压缩js,css文件,采用yuicompressor-maven-plugin插件进行压缩,但只是压缩减小大小,提高请求速度,并没有对js进行混淆.下面就写一下这个 ...
- GO语言学习(九)Go 语言运算符
运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...
- [Android5.1]ActivityManagerService启动过程分析
ActivityManagerService(简称AMS)是Android系统的关键服务之中的一个.它的主要作用例如以下: 管理系统中全部应用进程的整个生命周期 管理应用进程中的Activity.Se ...
- POJ 1932 XYZZY (ZOJ 1935)SPFA+floyd
http://poj.org/problem?id=1932 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1935 题目大 ...
- 小小ARC造福无数码农
今天无意中看到非常久之前的一个项目,古老的语法规范,还有更让人战战兢兢"内存管理代码"! 在这不得不说OC中内存管理的三种分类: Mannul Reference Counting ...
- 安装GDB和GDBSERVER
安装GDB和GDBSERVER 转自http://www.360doc.com/content/10/0407/17/155970_21971613.shtml 把GDBSERVER装入文件系统 转自 ...