美文网首页
[Leetcode]151. Reverse Words in

[Leetcode]151. Reverse Words in

作者: 木易yr | 来源:发表于2019-08-16 13:29 被阅读0次

151. Reverse Words in a String

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

Example 1:

Input: "the sky is blue"
Output: "blue is sky the"
Example 2:

Input: " hello world! "
Output: "world! hello"
Explanation: Your reversed string should not contain leading or trailing spaces.
Example 3:

Input: "a good example"
Output: "example good a"
Explanation: You need to reduce multiple spaces between two words to a single space in the reversed string.

Note:

A word is defined as a sequence of non-space characters.
Input string may contain leading or trailing spaces. However, your reversed string should not contain leading or trailing spaces.
You need to reduce multiple spaces between two words to a single space in the reversed string.

Follow up:
For C programmers, try to solve it in-place in O(1) extra space.

字符串水题
题意:就是翻转字符串中的每个单词,单词中间有空格,以下两种解法
1:用c++中的stringstream,如果在其他oj上需要加sstream头文件,很好用~

class Solution {
public:
   string reverseWords(string s) {
       stringstream ss(s);
       string res="",temp;
       //ss<<s;
       while(ss>>temp){
           res=" "+temp+res;
       }
       if(res!="")
           res.erase(res.begin());
       return res;
   }
};

这里补充一下sringstream的用法,从别的博客看到的,怕以后忘了,码一下~

    #include<iostream>  
    #include <sstream>   
    using namespace std;
    int main()
    {  
        string test = "-123 9.87 welcome to, 989, test!";  
        istringstream iss;//istringstream提供读 string 的功能  
        iss.str(test);//将 string 类型的 test 复制给 iss,返回 void   
        string s;  
        cout << "按照空格读取字符串:" << endl;  
        while (iss >> s){  
            cout << s << endl;//按空格读取string  
     }  
        cout << "*********************" << endl;  
      
        istringstream strm(test);   
        //创建存储 test 的副本的 stringstream 对象  
        int i;  
        float f;  
        char c;  
        char buff[1024];  
      
        strm >> i;  
        cout <<"读取int类型:"<< i << endl;  
        strm >> f;  
        cout <<"读取float类型:"<<f << endl;  
        strm >> c;  
        cout <<"读取char类型:"<< c << endl;  
        strm >> buff;  
        cout <<"读取buffer类型:"<< buff << endl;  
        strm.ignore(100, ',');  
        int j;  
        strm >> j;  
        cout <<"忽略‘,’读取int类型:"<< j << endl;  
      
        system("pause");  
        return 0;  
    }  

输出:

按照空格读取字符串:
-123
9.87
welcome
to,
989,
test!
. *********************
读取int类型:-123
读取float类型:9.87
读取char类型:w
读取buffer类型:elcome
忽略‘,’读取int类型:989
2:先翻转所有的单词,再翻转整个字符串,然后去掉多余的空格就可以了~

class Solution {
public:
    string reverseWords(string s) {
        int k = 0;  
        for(int i = 0; i < s.size(); i++){
            while(i < s.size() && s[i] == ' ') i++;//字符串常用
            if(i == s.size()) break;
            int j = i;
            while(j < s.size() && s[j] != ' ') j++;
            reverse(s.begin() + i, s.begin() + j);
            if(k) s[k++] = ' ';
            while(i < j) s[k++] = s[i++];
        }
        s.erase(s.begin() + k, s.end());
        reverse(s.begin(), s.end());
        return s;
    }
};

以上为本人以及参照大佬的学习记录,为了考试准备,如有错误欢迎指正~

相关文章

网友评论

      本文标题:[Leetcode]151. Reverse Words in

      本文链接:https://www.haomeiwen.com/subject/jbvfsctx.html