码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:Reverse Words in a String

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

标签:style   blog   http   color   io   os   ar   strong   for   

题目链接

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

标签:style   blog   http   color   io   os   ar   strong   for   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!