My c++ solution with a new idea

  • 0

    I count the number of 0,1,2 and set in vector in order.

    class Solution {
        void sortColors(vector<int>& nums) {
            int counts[3] = {0};
            for (auto it : nums) ++counts[it];
            for (int i = 0; i < 3; ++i)
                vector<int> v(counts[i], i);
                nums.insert(nums.end(), v.begin(), v.end());

Log in to reply

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