leetcode 翻转字符串

https://leetcode-cn.com/problems/reverse-words-in-a-string/

TLE代码:

class Solution {
public:
    string reverseWords(string s) {
        stringstream ss(s);
        string buf;
        vector<string> ans;
        while(ss<<buf){
            ans.push_back(buf);
        }
        for(vector<string>::iterator it = ans.end()-1;it==ans.begin();it++){
            cout<< *it<<" ";
        }
        return 0;
    }
};

亲测用stringstream确实挺慢的.

可以先用reverse 逆序,再挨个单词逆序回来.

猜你喜欢

转载自www.cnblogs.com/zhmlzhml/p/12676376.html