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

            }

    }

  • 相关阅读:
    SAP B1的几点不足
    对公司内审员培训的总结
    我们为了什么而活
    ERP实施一周总结
    SAP B1中物料主数据的术语解释
    好像回到了以前
    ERP总结
    WinHex
    Delphi和Windows主题相关的报错
    事件
  • 原文地址:https://www.cnblogs.com/whowhoha/p/5743289.html
Copyright © 2011-2022 走看看