LeetCode 151. Reverse Words in a String

时间:2021-01-29 06:24:23

题目

c++

class Solution {
public:
    string a[10005];
    string reverseWords(string s) {
        
        int j=0;
        string s2="";
        for(int i=0;i<s.length();i++)
        {
            if(s[i]==' ')
            {
                if(s2=="")
                {
                    continue;
                }
                else
                {
                    a[j++]=s2;
                    s2="";
                    continue;
                }
            }
            else
            {
                s2+=s[i];
            }
        }
        if(s2!="")
            a[j++]=s2;
        
        string res="";
        for(int i=j-1;i>=0;i--)
        {
            res+=a[i];
            if(i!=0)
                res+=" ";
        }
        
        return res;
            
    }
};