Leetcode Reverse Words in a String
2014-08-09 02:26
225 查看
这道题给出了两种解法。
一种是用一个数组保存发现的所有的word,然后在这些word中间加一个空格。具体做法是:扫描string, 得到word(如果当前字符是空格,则根据word的长度是否非0决定是否加入数组;如果不是空格,则word可以继续增长),然后倒着扫一遍数组生成一个新string,在word之间加入一个空格。这种方法的好处是空格完全由自己控制,对开头,结尾和原string word之间的多余空格可以很容易地过滤掉。
另一种是in place的做法。可以看http://www.geeksforgeeks.org/reverse-words-in-a-given-string/的解法描述。具体做法是先把每个词都reverse,然后把整个string reverse. 两遍reverse的结果是词出现的顺序reverse了,但是词本身没有reverse,符合题目要求。最后需要过一遍整个string,去除掉多余的空格。这一遍也可以通过覆盖原string从而in place完成。这种做法的好处是额外空间开销为constant,而第一种做法的空间开销为O(n).
一种是用一个数组保存发现的所有的word,然后在这些word中间加一个空格。具体做法是:扫描string, 得到word(如果当前字符是空格,则根据word的长度是否非0决定是否加入数组;如果不是空格,则word可以继续增长),然后倒着扫一遍数组生成一个新string,在word之间加入一个空格。这种方法的好处是空格完全由自己控制,对开头,结尾和原string word之间的多余空格可以很容易地过滤掉。
另一种是in place的做法。可以看http://www.geeksforgeeks.org/reverse-words-in-a-given-string/的解法描述。具体做法是先把每个词都reverse,然后把整个string reverse. 两遍reverse的结果是词出现的顺序reverse了,但是词本身没有reverse,符合题目要求。最后需要过一遍整个string,去除掉多余的空格。这一遍也可以通过覆盖原string从而in place完成。这种做法的好处是额外空间开销为constant,而第一种做法的空间开销为O(n).
#include <iostream> #include <vector> using namespace std; class Solution { public: void reverseWords(string &s) { std::vector<string> words; string word; for (int i = 0; i < s.size(); ++i) { if (s[i] != ' ') { word.push_back(s[i]); } else if (word.size() > 0) { words.push_back(word); word = ""; } } if (word.size() > 0) { words.push_back(word); } s = ""; for (int i = words.size() - 1; i >= 0; --i) { s += words[i]; if (i != 0) { s.push_back(' '); } } } void reverse(string &s, int start, int end) { for (int i = start, j = end; i < j; ++i, --j) { int tmp = s[i]; s[i] = s[j]; s[j] = tmp; } } void removeextraspaces(string &s) { int index = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] != ' ') { s[index] = s[i]; index++; } else if (index > 0 && s[index - 1] != ' ') { s[index] = ' '; index++; } } if (index >= 1 && s[index - 1] == ' ') { index--; } s = s.substr(0, index); } void reverseWords_v2(string &s) { int begin = 0; for (int i = 0; i < s.size(); ++i) { // if (begin < 0 && s[i] != ' ') // { // begin = i; // } if (s[i] == ' ') { reverse(s, begin, i - 1); begin = i + 1; } } // reverse the remaining word, if there is reverse(s, begin, s.size() - 1); // reverse the whole string, now only the order of word is reversed (instead of word itself) reverse(s, 0, s.size() - 1); // remove begining, trailing as well as multiple spaces between words removeextraspaces(s); } }; int main() { Solution sol; string s; s = " the sky is blue "; cout<<"["<<s<<"]"<<endl; sol.reverseWords_v2(s); cout<<"["<<s<<"]"<<endl; }
相关文章推荐
- Leetcode 第151题 最新题解 Reverse Words in a String
- leetcode JAVA Reverse Words in a String 难度系数2-3?最新题151
- LeetCode(151)Reverse Words in a String
- Leetcode: Reverse Words in a String
- LeetCode Reverse Words in a String
- 【LeetCode with Python】 Reverse Words in a String
- [LeetCode]Reverse Words in a String
- leetcode Reverse Words in a String
- [leetcode]Reverse Words in a String
- Leetcode_Reverse-Words-in-a-String
- LeetCode | Reverse Words in a String
- Reverse Words in a String (LeetCode)
- [Leetcode Solution] Reverse Words in a String
- LeetCode151:Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- [leetCode]Reverse Words in a String
- leetcode - Reverse Words in a String
- [LeetCode] Reverse Words in a String
- 【LeetCode】Reverse Words in a String
- 【LeetCode】Reverse Words in a String