c++ O(n)


  • 0
    P
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
             if(nums.size()==0)return;
             int i = 0, j = nums.size()-1;
             while(i < j){//put all 0 at front
                 while(i < j && nums[i] == 0)i++;
                 while(j >= 0 && nums[j] !=0 )j--;
                 swap(nums[i], nums[j]);
             }
             swap(nums[i],nums[j]);
             j = nums.size()-1;
             while(i < j) {//put all 1 after 0
                 while(i < j && nums[i] == 1)i++;
                 while(j >= 0 && nums[j] != 1)j--;
                 swap(nums[i], nums[j]);
             }
             swap(nums[i],nums[j]);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.