C++ solution, ugly but works...


  • 0
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int redcount=0;
            for(int i=0; i<nums.size()-redcount;i++)
            {
                if(nums[i]==0)
                {
                    nums.erase(nums.begin()+i);
                    nums.insert(nums.begin(),0);
                }
                else if(nums[i] == 2)
                {
                    nums.erase(nums.begin()+i);
                    nums.push_back(2);
                    i--;
                    redcount++;
                }
            }
        }
    };

Log in to reply
 

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