My O(n) solution using c++


  • 0
    L

    void sortColors(vector<int>& nums) {

        int m=0,n=0;
        for(vector<int>::iterator iter=nums.begin(); iter!=nums.end()-n;iter++){
            // cout<<nums[0]<<nums[1]<<nums[2]<<nums.size()<<endl;
            if(*iter==0){
                   nums.erase(iter);
                   //cout<<nums.size()<<endl;
                   nums.insert(nums.begin()+m,1,0);
                   //cout<<nums.size()<<endl;
                    m++;
               }
            if(*iter==2){
                nums.erase(iter);
                cout<<*iter<<endl;
    			iter--;
                nums.push_back(2);
                n++;
            }
        }
    }

Log in to reply
 

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