首页 > 其他 > 详细

151. Reverse Words in a String

时间:2018-08-10 20:22:45      阅读:144      评论:0      收藏:0      [点我收藏+]

题目描述:

Example:  

Input: "the sky is blue",
Output: "blue is sky the".

Note:

  • A word is defined as a sequence of non-space characters.
  • Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
  • You need to reduce multiple spaces between two words to a single space in the reversed string.

Follow up: For C programmers, try to solve it in-place in O(1) space.

解题思路:

题目不难,直接代码

代码:

 1 class Solution {
 2 public:
 3     void reverseWords(string &s) {
 4         reverse(s.begin(), s.end());
 5         istringstream sin(s);
 6         string word, res;
 7         while (sin>>word) {
 8             reverse(word.begin(), word.end());
 9             res += word;
10             res += " ";
11         }
12         if (res.size() > 0)
13             res.erase(res.size() - 1, 1);
14         s = res;
15     }
16 };

 

151. Reverse Words in a String

原文:https://www.cnblogs.com/gsz-/p/9457126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!