C++ counters solution


  • 0
    S
    class Solution {
    public:
        void sortColors(vector<int>& nums) {
            vector<int> a(3); // number of colors
            for (auto& num : nums) {
                a[num]++; 
            }
            int y = 0;     // fixed part of array
            for (int color = 0; color < 3; ++color) {
                for (int i = 0; i < a[color]; ++i) {
                    nums[y + i] = color; 
                }
                y += a[color];
            }        
            return;
        }
    };

Log in to reply
 

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