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

            }

    }

  • 相关阅读:
    69道Java Spring 面试&笔试题
    不少程序员都会遇到的三个问题
    Java中main方面面试题
    学生成绩表(对成绩的操作)
    Eclipse常用快捷键
    java 调用打印机 打印服务
    java 打开浏览器 url
    java 邮件发送 apache commons-email
    java 邮件收发 (只能输入英文,中文需要转码)
    jquery 画板折叠
  • 原文地址:https://www.cnblogs.com/whowhoha/p/5743289.html
Copyright © 2011-2022 走看看