share my c++ simple solution


  • 0
    C

    Because there are just 3 types of data. So we can use an array of size 3 to record the data. And we reassign the value into the data.

    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            int a[3] = {0,0,0};
            for(int n :nums){
                a[n]++;
            }
            int idx = 0;
            for( int i = 0; i<3; i++){
                while( a[i]-- > 0){
                    nums[idx++] = i;
                }
            }
        }
    };
    

Log in to reply
 

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