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

leetcode6 Reverse Words in a String 单词取反

时间:2016-08-06 09:53:53      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

   Reverse Words in a String  单词取反

    whowhoha@outlook.com

Question:

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

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

void reverseWords(string &s) {

        vector <string>des;

        if(s.empty())

                 return;

        int len = s.size();

        for(int i = 0; i < len; i++){

                 if(s[i] == ‘ ‘)

                         continue;

                 string word;

                 while(i < len && s[i] != ‘ ‘){

                         word += s[i];

                         i++;

                 }

                 des.push_back(word);

        }

        reverse(des.begin(), des.end());

        if(des.empty())

                 s = "";

        else {

                 s.clear();

                 int j ;

                 for(j = 0; j < des.size() -1; j++){

                         s += des[j];

                         s += ‘ ‘;

                 }

                 s += des[j];   

        }

}

leetcode6 Reverse Words in a String 单词取反

标签:

原文地址:http://www.cnblogs.com/whowhoha/p/5743289.html

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