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 ...
随机推荐
- 【转载】VMware虚拟机修改硬盘容量大小
很多人在安装虚拟机系统的时候,为了节省硬盘空间,把硬盘容量设置得较小,可是后来发现硬盘容量不够用了.在VMware中又不能直接修改虚拟机的硬盘容量大小,或者重建虚拟机系统,非常麻烦. 其实在VMwar ...
- js动画之简单运动二
透明度的变化 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 排序小结(C++版)
一.快速排序 #include <iostream> using namespace std; int adjust(int a[],int start,int end) { int i, ...
- Ubuntu下快速安装LAMP server
Ubuntu下可快速安装LAMP server(Apache+MySQL+PHP5). 首先,打开Ubuntu虚拟机,Terminal打开root权限:“sudo -s”. 一.安装LAMP serv ...
- java基础介绍(转)
很全面的介绍JAVA基础的文档 Java 编程简介,第 1 部分: Java 语言基础 http://www.ibm.com/developerworks/cn/java/j-introtojava1 ...
- MySQL表类型和存储引擎版本不一致解决方法
使用的是老版本的mysql客户端Navicate 8 ,mysql 服务端用的是mysql5.6的版本,在修改版本引擎的时候出现版本不对; mysql error ‘TYPE=MyISAM’ 解决办法 ...
- Bitcode设置 编译问题
今天在一个iOS培训网站上看到一篇关于第三方库不包含bitcode就会报错的文章,感觉剖析得很详细,分享出来,希望可以对iOS初入门者有所帮助.下面我们就一起来看看吧. 用Xcode 7 beta 3 ...
- matlab使用
1.nargin 在matlab中定义一个函数时, 在函数体内部, nargin是用来判断输入变量个数的函数. 2.assert “断言”,“坚持自己的主张”.判断函数. http://www.cnb ...
- php wampserver 80 端口无法开启的解决方法
下载Microsoft Visual C++ 2005 Redistributable Package x86 和 x64(vc_redist.x86.exe/vc_redist.x64.exe) 安 ...
- JS判断是否来自手机移动端的访问,并跳转
var browserName_ = navigator.userAgent ; &&browserName_.indexOf(&&browserName_.index ...