LeetCode 75 --- Sort Colors

题目链接:
LeetCode 75 — Sort Colors

AC代码:

public class Problem75 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }
    public class Solution {
        public void sortColors(int[] nums) {
            int a=0,b=0,c=0;
            for(int i=0;i<nums.length;i++){
                if(nums[i]==0) a++;
                else if(nums[i]==1) b++;
                else c++;
            }
            for(int i=0;i<a;i++)
                nums[i]=0;
            for(int i=a;i<a+b;i++)
                nums[i]=1;
            for(int i=a+b;i<a+b+c;i++)
                nums[i]=2;

        }
    }

}

你可能感兴趣的:(LeetCode,算法)