C++ one pass solution xD


  • 0
    I
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int red = -1;
            int blue = nums.size();
            int i = 0;
            while (i < blue) {
                if (nums[i] == 0) {
                    red++;
                    swap(nums[i], nums[red]);
                    i++;
                } else if (nums[i] == 2) {
                    blue--;
                    swap(nums[i], nums[blue]);
                } else {
                    i++;
                }
            }
        }
    };
    

Log in to reply
 

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