Clean JAVA code


  • 0
    A
    public void reverseWords(char[] s) {
    	int i = 0;
    	while (i < s.length) {
    		if (s[i] != ' ') {
    			int l = i;
    			while (i < s.length && s[i] != ' ')i++;
    			reverse(s, l, i - 1);
    		} else i++;
    	}
    	reverse(s, 0, s.length - 1);
    }
    
    void reverse(char[] s, int l, int r) {
    	while (l < r) {
    		char t = s[l];
    		s[l++] = s[r];
    		s[r--] = t;
    	}
    }

Log in to reply
 

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