Easy Understand Java Solution


  • 0
    W
    public void sortColors(int[] nums) {
            int start = 0, end = nums.length - 1;
            for (int i = 0; i <= end; i++) {
                if (nums[i] == 0) {
                    nums[i] = nums[start];
                    nums[start] = 0;
                    start++;
                } else if (nums[i] == 2) {
                    nums[i] = nums[end];
                    nums[end] = 2;
                    i--;
                    end--;
                }
            }
        }
    

Log in to reply
 

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