252 ms java solution


  • 0
    Y
    public class Solution {
    public void reverseWords(char[] s) {
        if(s.length == 0) return;
        rotate(s,0,s.length-1);
        int start = 0;
        int end = 0;
        for(int i = 0; i < s.length; i++) {
            if(s[i] == ' ') {
                end = i-1;
                rotate(s, start, end);
                start = i+1;
                i++;
            }
        }
        rotate(s, start, s.length-1);
    }
    public void rotate(char[] s, int start, int end) {
        while(start < end) {
            char temp = s[start];
            s[start] = s[end];
            s[end] = temp;
            start++;
            end--;
        }
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.