class Solution {
public:
    void reverseString(vector<char>& s) {
        int  L=0,R=s.size()-1;
        while(L<R){
            swap(s[L++],s[R--]);
        }
    }
};

输入:["h","e","l","l","o"]
输出:["o","l","l","e","h"]

几秒前

​通过​

108 ms

17.8 MB

cpp

 

class Solution {
public:
    string reverseWords(string s) {
        int len=s.length();
        string sum="";
        int i=0;
        while(i<len){
            string tmp="";
            while(i<len &&s[i]!=' '){
                tmp+=s[i];
                i++;
                
            }
            reverse(tmp.begin(),tmp.end());
            sum+=tmp;
            if(i<len){
                sum+=' ';
                i++;
            }
            
        }
            
            return sum;
        }
};


输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 


几秒前

​通过​

60 ms

15.4 MB

cpp