C++ Solution: similar to 3-road quick-sort


  • 0
    K
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int zero = -1; // 
            int two = nums.size();
            int one  = 0;
            while(one < two) {
                if(nums[one] == 1) one++;
                else if (nums[one] == 2) swap(nums[one], nums[--two]);
                else {
                    assert(nums[one] == 0);
                    swap(nums[one++], nums[++zero]);
                }
            } 
        }
    };
    

Log in to reply
 

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