An easy to understand java solution


  • 0
    Z

    public class Solution {
    public void reverseWords(char[] s) {
    int len = s.length;
    int cur = 0, right;

        reverse(s , 0, len - 1);
        
        while(cur < len) {
            right = cur;
            
            while(right < len && s[right] != ' ') {
                right++;
            }
            
            reverse(s , cur , right - 1);
            
            cur = right + 1;
        }
    }
    
    public void reverse(char[] s, int left, int right) {
        
        char c;
        
        while(left < right) {
            c = s[left];
            s[left] = s[right];
            s[right] = c;
            left++;
            right--;
        }
    }
    

    }


Log in to reply
 

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