Easy java solution~~1ms


  • 0
    D

    public class Solution {
    public void sortColors(int[] nums) {
    int start=0,j=nums.length-1;
    for(int i=0;i<nums.length;i++)
    {
    while(nums[i]==2&&i<j)
    {
    swap(nums,i,j--);
    }
    while(nums[i]==0&&start<i)
    {
    swap(nums,i,start++);
    }

        }
        
    }
    public void swap(int[]nums,int a,int b)
    {
        int temp=nums[a];
        nums[a]=nums[b];
        nums[b]=temp;
    }
    

    }


Log in to reply
 

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