7. 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
".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.
解题思路: 先整体翻转,在把单词一个个翻转。(或反过来也行)
代码也许还能优化:
class Solution {
public:
void reverseWords(string &s) {
if(s == "") return;
if(s.length() == popBlankLead(s)) return;
else if(popBlankTrail(s) < 0) return;
int judge = 0;
while(judge < s.length() && s[judge] != ' ') ++judge;
if(judge == s.length()) return; reverseAlpha(s, 0, s.length() - 1);
int start = 0;
for(int end = 0; end < s.length(); ++end){
if(s[end] == ' '){
while(s[end + 1] == ' '){
s.erase(end, 1);
}
reverseAlpha(s, start, end - 1);
start = end + 1;
}
}
reverseAlpha(s, start, s.length() - 1);
}
void reverseAlpha(string &s, int begin, int end){
if(s == "" || begin >= end) return;
int mid = (end + begin +1) >> 1;
for(int i = begin; i < mid; ++i){
char c = s[i];
s[i] = s[end];
s[end--] = c;
}
}
int popBlankLead(string &s){
int first = 0;
while(s[first] == ' ' && first < s.length()) ++first;
s.erase(0, first);
return first;
}
int popBlankTrail(string &s){
int end = s.length() - 1;
while(s[end] == ' ' && end >= 0) --end;
s.erase(end + 1,s.length() - end - 1);
return end;
}
};
精简版代码:
class Solution {
public:
void reverseWords(string &s) {
string buf;
stringstream ss(s);
vector<string> tokens;
while (ss >> buf) tokens.push_back(buf);
if (tokens.size() == 0) s="";
else{
int n = tokens.size()-1;
s = tokens[n];
for (int i = n-1; i >=0; -- i) s+=" "+tokens[i];
}
}
};
7. Reverse Words in a String的更多相关文章
- [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 II 翻转字符串中的单词之二
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space ...
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...
- [LintCode] 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 II
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-ii/ 题目: Given an input string, rever ...
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation Evaluate the value of an arithm ...
- leetcode6 Reverse Words in a String 单词取反
Reverse Words in a String 单词取反 whowhoha@outlook.com Question: Given an input string s, reverse the ...
- leetcode面试准备:Reverse Words in a String
leetcode面试准备:Reverse Words in a String 1 题目 Given an input string, reverse the string word by word. ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- 【LeetCode练习题】Reverse Words in a String
Reverse Words in a String Given an input string, reverse the string word by word. For example,Given ...
随机推荐
- CPU指令系统
CPU就是通过指令系统来操控寄存器然后实现读取数据的,所以我们必须介绍一下CPU的指令系统 如果我们知道指令的英文全称,这对我们理解指令的作用有很大帮助,所以贴出指令英文全称 接下来就是介绍一些主要的 ...
- Android Studio的一些快捷键
以下这些也是百度的其他人整理的.后面有新的会加进来. AS的快捷键容易和QQ,微信等冲突,可以手动关掉或者修改其他软件的热键 Ctrl+G / Ctrl+Alt+Shift+G:查询变量或者函数或者类 ...
- 超级链接a中javascript:void(0)弹出另外一个框问题
转字:http://my.oschina.net/castusz/blog/68186 结果在IE.Firefox.Chrome都是先执行的onclick事件,在项目中我们尽量不要同时使用这两种方式. ...
- C#代码示例_函数
参数数组 C#允许为函数指定一个(只能指定一个)特定的参数,这个参数必须是函数定义中的最后一个参数,称为参数数组.参数数组可以使用个数不定的参数调用函数,可以使用params关键字定义它们. 参数数组 ...
- javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
原因很简单:因为在js中{}表示一个语句块(代码段),所有加上"()"表示表达式
- 限制文本框只能输入数字或浮点数的JS脚步
1.oninput,onpropertychange,onchange的用法 l onchange触发事件必须满足两个条件: a)当前对象属性改变,并且是由键盘或鼠标事件激发的(脚本 ...
- 极简版 react+webpack 脚手架
目录结构 asset/ css/ img/ src/ entry.js ------------------------ 入口文件 .babelrc index.html package.json w ...
- Day20_IO第二天
1.IO体系总图 2.IO体系--字节流 记忆路线:输入输出流前面加File和Buffered,这样就全记住了 3.表达式解释 表达式:由变量和常量通过运算符连接起来的式子,单个的常量和变量也是表达式 ...
- HTML5实现屏幕手势解锁
HTML5实现屏幕手势解锁(转载) https://github.com/lvming6816077/H5lockHow to use? <script type="text/java ...
- EntityFramework 实体映射到数据库
EntityFramework实体映射到数据库 在Entity Framework Code First与数据表之间的映射方式实现: 1.Fluent API映射 通过重写DbContext上的OnM ...