颜色分类 (Sort Colors)

 

思路:

// @Title: 颜色分类 (Sort Colors)
// @Author: qisiii
// @Date: 2020-11-18 14:35:20
// @Runtime: 0 ms
// @Memory: 36.8 MB
// @comment: 
// @flag: 
class Solution {
    public void sortColors(int[] nums) {
        if(nums.length<=1){
            return;
        }
        int p0=0,p2=nums.length-1;
        for(int i=0;i<=p2;i++){
            if(nums[i]==0){
                swap(nums,i,p0);
                p0++;
            }
            if(nums[i]==2){
                swap(nums,i,p2);
                p2--;
                if(nums[i]!=1){
                    i--;
                }
            }
        }
    }
    public void swap(int[] nums,int a,int b){
        int temp=nums[a];
        nums[a]=nums[b];
        nums[b]=temp;
    }
}

+++ title = “颜色分类 (Sort Colors)” draft = false +++

思路:

// @Title: 颜色分类 (Sort Colors)
// @Author: qisiii
// @Date: 2020-11-18 14:38:26
// @Runtime: 0 ms
// @Memory: 36.9 MB
// @comment: 
// @flag: 
class Solution {
    public void sortColors(int[] nums) {
        if(nums.length<=1){
            return;
        }
        int p0=0,p2=nums.length-1;
        int count=0;
        for(int i=0;i<=p2;i++){
            count++;
            if(nums[i]==0){
                swap(nums,i,p0);
                p0++;
            }
            if(nums[i]==2){
                swap(nums,i,p2);
                p2--;
                if(nums[i]!=1){
                    i--;
                }
            }
        }
    }
    public void swap(int[] nums,int a,int b){
        int temp=nums[a];
        nums[a]=nums[b];
        nums[b]=temp;
    }
}

思路:

// @Title: 颜色分类 (Sort Colors)
// @Author: qisiii
// @Date: 2020-11-18 14:38:10
// @Runtime: 2 ms
// @Memory: 37 MB
// @comment: 
// @flag: 
class Solution {
    public void sortColors(int[] nums) {
        if(nums.length<=1){
            return;
        }
        int p0=0,p2=nums.length-1;
        int count=0;
        for(int i=0;i<=p2;i++){
            count++;
            if(nums[i]==0){
                swap(nums,i,p0);
                p0++;
            }
            if(nums[i]==2){
                swap(nums,i,p2);
                p2--;
                if(nums[i]!=1){
                    i--;
                }
            }
        }
        System.out.println(count);
    }
    public void swap(int[] nums,int a,int b){
        int temp=nums[a];
        nums[a]=nums[b];
        nums[b]=temp;
    }
}

思路:

// @Title: 颜色分类 (Sort Colors)
// @Author: qisiii
// @Date: 2020-11-18 14:38:37
// @Runtime: 0 ms
// @Memory: 37.1 MB
// @comment: 
// @flag: 
class Solution {
    public void sortColors(int[] nums) {
        if(nums.length<=1){
            return;
        }
        int p0=0,p2=nums.length-1;
        for(int i=0;i<=p2;i++){
            if(nums[i]==0){
                swap(nums,i,p0);
                p0++;
            }
            if(nums[i]==2){
                swap(nums,i,p2);
                p2--;
                if(nums[i]!=1){
                    i--;
                }
            }
        }
    }
    public void swap(int[] nums,int a,int b){
        int temp=nums[a];
        nums[a]=nums[b];
        nums[b]=temp;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18