Leet_Code_75_SortColor

public void sortColors(int[] nums) {
        int left = 0;
        int right = nums.length-1;
        for (int i=0;i<=right;i++) {
            if (nums[i] == 0) {
                swap(i,left,nums);
                left++;
            } else if (nums[i] == 2){
                swap(i,right,nums);
                i--;
                right--;
            }
        }
        
    }
    
    public void swap(int i, int j, int[] nums) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }

 

posted on 2021-03-17 07:55  MaXianZhe  阅读(32)  评论(0编辑  收藏  举报

导航