zoukankan      html  css  js  c++  java
  • leetcode6 Reverse Words in a String 单词取反

       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];   

            }

    }

  • 相关阅读:
    ElasticSearch 2 (23)
    ElasticSearch 2 (22)
    微信小程序框架模板部署:mpvue2.x+typescript+webpack3.x
    mpvue添加对scss的支持
    mpvue 封装axios请求方法
    Vue中qs插件的使用
    在微信小程序中使用less/sass
    微信小程序封装request请求
    VSCode --tsc : 无法加载文件
    Vue项目中的RSA加解密
  • 原文地址:https://www.cnblogs.com/whowhoha/p/5743289.html
Copyright © 2011-2022 走看看