Question

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" Note: In the string, each word is separated by single space and there will not be any extra space in the string.


Solution

class Solution {
public:
    string reverseWords(string s) {
        if (s.size() == 0) return s;
        for (int left = 0, right = 0; right <= s.size()-1; right++){
            if(right == s.size() - 1) reverse(s.begin()+left, s.end());
            if(s[right] == ' ') {
                reverse(s.begin()+left, s.begin()+right);
                left = right + 1;
            }
        }
        return s;
    }
};
By guozetang            Updated: 2020-09-19 13:02:30

results matching ""

    No results matching ""