75. Sort Colors

public class Solution {
    public void sortColors(int[] nums) {
        int lo=0;
        int hi=nums.length-1;
        int idx=0;
        while(idx<=hi)
        {
            if(nums[idx]==0)
            {
                nums[idx]=nums[lo];
                nums[lo]=0;
                lo++;
            }
            else if(nums[idx]==2)
            {
                nums[idx]=nums[hi];
                nums[hi]=2;
                hi--;
                idx--;
            }
            idx++;
        }
    }
}

 

posted @ 2017-09-26 12:01  Weiyu Wang  阅读(93)  评论(0编辑  收藏  举报