3ms - Single pass 6 lines solution in C++


  • 0
    I
    void sortColors(vector<int>& nums) {
        int start = 0, end = nums.size() - 1;
        for (int i = 0; i < nums.size(); i++)
            if (nums[i] == 0 && start < i)
                swap(nums[i--], nums[start++]);
            else if (nums[i] == 2 && end > i)
                swap(nums[i--], nums[end--]);
    }
    

Log in to reply
 

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