[LintCode] Reverse Words in a String 翻转字符串中的单词
2016-11-15 14:20
537 查看
Given an input string, reverse the string word by word.
For example,
Given s = "
return "
Have you met this question in a real interview?
Yes
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.
LeetCode上的原题,请参见我之前的博客Reverse Words in a String。
解法一:
解法二:
解法三:
For example,
Given s = "
the sky is blue",
return "
blue is sky the".
Have you met this question in a real interview?
Yes
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.
LeetCode上的原题,请参见我之前的博客Reverse Words in a String。
解法一:
class Solution { public: /** * @param s : A string * @return : A string */ string reverseWords(string s) { int storeIndex = 0, n = s.size(); reverse(s.begin(), s.end()); for (int i = 0; i < n; ++i) { if (s[i] != ' ') { if (storeIndex != 0) s[storeIndex++] = ' '; int j = i; while (j < n && s[j] != ' ') s[storeIndex++] = s[j++]; reverse(s.begin() + storeIndex - (j - i), s.begin() + storeIndex); i = j; } } return string(s.begin(), s.begin() + storeIndex); } };
解法二:
class Solution { public: /** * @param s : A string * @return : A string */ string reverseWords(string s) { string res = "", t = ""; istringstream is(s); while (getline(is, t, ' ')) { if (t.empty()) continue; res = (res.empty() ? t : (t + " " + res)); } return res; } };
解法三:
class Solution { public: /** * @param s : A string * @return : A string */ string reverseWords(string s) { istringstream is(s); is >> s; string t; while (is >> t) { s = t + " " + s; } return (s[0] == ' ') ? "" : s; } };
相关文章推荐
- 【LintCode】Reverse Words in a String 翻转字符串
- LeetCode OJ 之 Reverse Words in a String (翻转字符串的单词)
- Reverse Words in a String 翻转一个字符串里的单词顺序 @LeetCode
- Reverse Words in a String III 翻转字符串中的单词之三
- 151. 翻转字符串里的单词(Reverse Words in a String)
- [LeetCode] Reverse Words in a String 翻转字符串中的单词
- LeetCode刷题:Reverse Words in a String(翻转字符串中的单词)
- leetcode151-Reverse Words in a String(翻转字符串单词的位置)
- [LeetCode] Reverse Words in a String III 翻转字符串中的单词之三
- 【LintCode】Reverse Words in a String 翻转字符串
- 【LeetCode-面试算法经典-Java实现】【151-Reverse Words in a String(反转字符串中的单词)】
- 字符串算法——反转字符串单词表(Reverse Words in a String)
- 字符串-Reverse Words in a String(翻转字符串)
- LeetCode | Reverse Words in a String(字符串中的单词序反转)
- Reverse Words in a String 翻转字符串
- Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度
- 151.leetcode Reverse Words in a String(medium)[字符串分词翻转]
- Reverse Words in a String 翻转句子的单词
- leetcode——Reverse Words in a String 旋转字符串中单词顺序(AC)
- LeetCode:151_Reverse Words in a String | 字符串中单词的逆反 | Medium