LeetCode-Reverse Words in a String
2014-04-01 12:59
253 查看
Given an input string, reverse the string word by word.
For example,
Given s = "
return "
click to show clarification.
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.
For example,
Given s = "
the sky is blue",
return "
blue is sky the".
click to show clarification.
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) { stringstream ss; ss<<s; string term; string ret=""; if(!ss.eof()) { ss>>term; ret=term; } term=""; while(!ss.eof()){ term=""; ss>>term; if(term=="")continue; ret=term+" "+ret; } s=ret; } };
相关文章推荐
- LeetCode---Reverse Words in a String
- [LeetCode]Reverse Words in a String
- LeetCode 151. Reverse Words in a String
- Reverse Words in a String--LeetCode
- LeetCode 1 Reverse Words in a String
- LeetCode--Reverse Words in a String
- [LeetCode]Reverse Words in a String
- [leetcode] Reverse Words in a String [1]
- LeetCode(151)Reverse Words in a String
- 【LeetCode刷题Java版】Reverse Words in a String
- Reverse Words in a String --leetcode
- 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, python] Reverse Words in a String 反转字符串
- LeetCode | Reverse Words in a String
- LeetCode - Refresh - Reverse Words in a String II
- LeetCode:Reverse Words in a String
- [LeetCode] Reverse Words in a String