My simple 2 for loop Java solution


  • 0
    public class Solution {
        public void reverseWords(char[] s) {
            for (int i = 0, j = 0; i < s.length; i++) {
                for (j = i; i < s.length && s[i] != ' '; i++);
                reverse(s, j, i - 1);
            }
            reverse(s, 0, s.length - 1);
        }
        
        private void reverse(char[] s, int i, int j) {
            for (; i < j; i++, j--) {
                char c = s[i];
                s[i] = s[j];
                s[j] = c;
            }
        }
    }
    

    The process is straightforward, we first reverse every word, then reverse whole array.


Log in to reply
 

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