151. Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
Update (2015-02-12):
For C programmers: Try to solve it in-place in O(1) space.
click to show clarification.
- 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.
=====================
注意,
1,被空格包围的是单词
2,输入字符串可以以空格开头或结尾,但是结果中的字符不能以空格开头或结尾
3,输出字符串中单词间的空格是一个,不能重复出现空格.
思路:
对输入字符串进行去重空格操作,
对字符串中的每个单词进行反转
对整个字符串进行反转
====
code
class Solution {
public:
void help_reverse(string &s,int start,int end){
while(start<end){///经典的反转字符串方法
swap(s[start++],s[end--]);
}
}
string removeDuplicateSpace(string s){
string res;
int b = ;
for(;b<(int)s.size();b++){
if(s[b]!= ' '){
break;
}
}///
int e = s.size() - ;
for(;e>=;e--){
if(s[e]!=' '){
break;
}
} bool is_space = false;
for(int i = b;i<=e;i++){
if(s[i] == ' '){
if(!is_space) is_space = true;
else continue;
}else{
is_space = false;
}
res.push_back(s[i]);
}
return res;
}
void reverseWords(string &s) {
if(s.empty()) return;
s = removeDuplicateSpace(s);
int start = ;
for(size_t i = ;i<s.size();i++){
if(s[i]!=' '){
start = i;
}else{
continue;
}
size_t j = i;
while(j<s.size() && s[j]!=' '){
j++;
}
j--;
help_reverse(s,start,j);
i = j++;
}
help_reverse(s,,(int)s.size()-);
}
};
151. Reverse Words in a String的更多相关文章
- leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
557. Reverse Words in a String III 最简单的把空白之间的词反转 class Solution { public: string reverseWords(string ...
- 151. Reverse Words in a String(java 注意细节处理)
题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...
- [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】151 Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: ...
- (String)151. Reverse Words in a String
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- Java for 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 151. Reverse Words in a String --------- java
Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...
- 151. Reverse Words in a String翻转一句话中的单词
[抄题]: Given an input string, reverse the string word by word. Example: Input: "the sky is blue& ...
- 151. Reverse Words in a String (String)
思路: 本题考查的目的并不是使用字符串的函数.方法是两次reverse,先对每个单词先做一次翻转,然后对整个字符串做一次翻转. 需要注意的是去除extra space,并且对全space字符串.以及最 ...
随机推荐
- Baxter机器人---Hello_baster(二)
原创博文,转载请标明出处:--周学伟http://www.cnblogs.com/zxouxuewei/ Step 1: Setup ROS Environment root@zxwubuntu-As ...
- Codeforces Round #121 (Div. 2)
A. Funky Numbers 记\(a \le b\),枚举\(a\)即可. B. Walking in the Rain 二分时间,然后\(dp(i)\)表示是否能从1到达i. C. Dynas ...
- Android概述(思维导图)
- 主机连接虚拟机 web服务
主机上安装了个VM centeros 首先通过 终端:ifconfig eth0 查看虚拟机的Ip地址 这样就能在主机上通过浏览器http://192.168.150.128/a.php 访问虚拟机的 ...
- 【转】完美解除Windows7的驱动程序强制签名限制
原文网址:http://nick.txtcc.com/index.php/nocategory/290 Windows 7很J,很多驱动程序都无法安装,因为Windows 7不像Vista,必须要求所 ...
- COM ,Threading Models,apartments,RPC
Component Object Model (COM) https://msdn.microsoft.com/en-us/library/windows/desktop/ms680573%28v=v ...
- 矩阵的QR分解(三种方法)Python实现
1.Gram-Schmidt正交化 假设原来的矩阵为[a,b],a,b为线性无关的二维向量,下面我们通过Gram-Schmidt正交化使得矩阵A为标准正交矩阵: 假设正交化后的矩阵为Q=[A,B],我 ...
- PowerDesigner的图形工具栏被我关了 怎么才能恢复?就是那个快捷工具栏 图形那个里面有什么放大镜 表 视图什么的
PowerDesigner的图形工具栏被我关了 怎么才能恢复?就是那个快捷工具栏 图形那个里面有什么放大镜 表 视图什么的 工具-->自定义工具栏-->palette选中
- SVG ViewBox
如果svg图形太大或者太小,就可以用ViewBox属性来调整在页面中的显示范围.大小. "像素不能直接换算成英寸.厘米,要在 dpi ( dot per inch 分辨率,概念较多,鼠标 d ...
- wikioi 1204 寻找子串位置
/*======================================================================== 1204 寻找子串位置 题目描述 Descript ...