Sunday, 20 December 2015

LeetCode Sort Colors

LeetCode Sort Colors

class Solution {

public:

    void sortColors(vector<int>& nums) {

        int n=nums.size(),i;

        int count0=0,count1=0,count2=0;

        for(i=0;i<n;i++)

         {

             switch(nums[i])

             {

                 case 0:

                   (count0)++;

                   break;

                 case 1:

                   (count1)++;

                   break;

                 case 2:

                   (count2)++;

                   break;

                 default:

                    ;

             }

         }

        i=0;

        while((count0)--)

          nums[i++]=0;

        while((count1)--)

          nums[i++]=1;

        while((count2)--)

          nums[i++]=2;

    }

};

No comments:

Post a Comment