leetcode解题报告(25):Reverse Words in a String III
2017-05-17 14:32
645 查看
描述
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.
Example 1:
Input: "Let's take LeetCode contest"
Output: "s'teL ekat edoCteeL tsetnoc"
Note: In the string, each word is separated by single space and there will not be any extra space in the string.
分析
用istringstream来读取string中的每一个word,然后对每一个word通过reverse函数反转。代码如下:
class Solution { public: string reverseWords(string s) { string word; //hold every word in the string string ret; istringstream ss(s); while(ss>>word){ //read a word reverse(word.begin(),word.end()); //reverse it ret = ret + word + " "; //splice them } return ret.substr(0,ret.size() - 1); //remove ' ' at end of the string } };
相关文章推荐
- 【LeetCode】Reverse Words in a String III 解题报告
- leetcode解题报告 Reverse Words in a String
- LeetCode 解题报告 Reverse Words in a String
- [leetcode]151. Reverse Words in a String@Java解题报告
- 【LeetCode】Reverse Words in a String 解题报告
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
- [Leetcode] 557. Reverse Words in a String III 解题报告
- LeetCode 557 Reverse Words in a String III(逆转字符串)
- leetcode 557 Reverse Words in a String III C++
- LeetCode之Reverse Words in a String III
- Leetcode——557. Reverse Words in a String III
- leetcode 557 Reverse Words in a String III
- leetcode解题方案--151--Reverse Words in a String
- leetcode[Reverse Words in a String III]//待整理多种解法
- leetCode---Reverse Words in a String III
- leetcode557——Reverse Words in a String III
- leetcode-Reverse Words in a String III
- 【LeetCode】557 Reverse Words in a String III
- [LeetCode OJ] Reverse Words in a String 解题报告
- leetcode557. Reverse Words in a String III