首页 > 其他 > 详细

LeetCode:Reverse Words in a String

时间:2014-09-22 20:09:34      阅读:129      评论:0      收藏:0      [点我收藏+]

题目链接

Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".


 1 class Solution {
 2 public:
 3     void reverseWords(string &s) 
 4     {
 5         //从前往后扫描 
 6         string res, word;
 7         for(int i = s.size()-1; i >= 0;)
 8         {
 9             while(i >= 0 && s[i] ==  )--i;//去掉空格
10             if(i < 0)break;
11             if(res.size() != 0)res.push_back( );
12             word.clear();
13             while(i >= 0 && s[i] !=  )word.push_back(s[i--]);//word为找到的一个单词
14             for(int j = word.size()-1; j >= 0; --j)
15                 res.push_back(word[j]);
16         }
17         s = res;
18     }
19 };

 

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3986615.html

LeetCode:Reverse Words in a String

原文:http://www.cnblogs.com/TenosDoIt/p/3986615.html

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