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 ...
随机推荐
- python基础--数值类型和序列类型
Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1 #任意整数 float:2.3 #小数 python赋值: a = ...
- 分治法(divide & conquer)与动态规划(dynamic programming)应用举例
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...
- (转)DOS批处理的字符串功能
批处理有着具有非常强大的字符串处理能力,其功能绝不低于C语言里面的字符串函数集.批处理中可实现的字符串处理功能有:截取字符串内容.替换字符串特定字段.合并字符串.扩充字符串等功能.下面对这些功能一一进 ...
- comparator接口与Comparable接口的差别
1. Comparator 和 Comparable 同样的地方 他们都是java的一个接口, 而且是用来对自己定义的class比較大小的, 什么是自己定义class: 如 public class ...
- gvim不能直接打开360压缩打开的文件
1. 压缩文件a.rar 2. 默认使用360压缩打开 3.用gvim打开对应的a.c文件,提示permission denied 4.用gvim跟踪目录,发现360管理的缓冲目录无法打开 原因未分析 ...
- C语言深度剖析-----数组参数和指针参数分析
数组退化的意义 当向函数传递数组时, 二维数组参数 等价关系 注意事项 只能去一维数组 无法向一个函数传递一个任意的多维数组,注释地方出错 传递与访问二维数组的方式 动态地算出二维数组的列
- Altium Designer如何移动选中的所有对象
用shift去选中, 快捷键M S移动整体
- 使用DNSCrypt解决Dropbox污染问题
作者:半点闲 时间:2014-6-27 18:27 博客:blog.csdn.net/cg_i 邮箱:b_dx@sohu.com 背景知识:防火长城(GFW) keyword:DNSCrypt ...
- ScrollView ViewPager ListView三者共存的问题
随喜结佛缘 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcWlueXVueWluZw==/font/5a6L5L2T/fontsize/400/fill/ ...
- [RxJS] Hot Observable, by .share()
.share() is an alias for .publish().refCount(). So if the source is not yet completed, no matter how ...