public class Solution 
{
    public void sortColors(int[] nums) 
    {
        int red = 0;
        int blue = nums.length-1;
        
        for(int i=0; i<=blue; i++)
        {
            if(nums[i] == 0) // if find 0, swap with red pointer
            {
                int temp = nums[i];
                nums[i] = nums[red];
                nums[red] = temp;
                
                red++;
            }
            else if(nums[i] == 2) // if find 2, swap with blue pointer
            {
                int temp = nums[i];
                nums[i] = nums[blue];
                nums[blue] = temp;
                
                i--;
                blue--;
            }
       
        }
    }
}

相关文章:

  • 2021-05-20
  • 2022-01-13
  • 2022-01-20
  • 2021-06-30
  • 2021-10-23
  • 2021-10-30
猜你喜欢
  • 2022-03-02
相关资源
相似解决方案