[c#] Counting-Sort


  • 0
    C
    public class Solution {
        public void SortColors(int[] nums) {
            var count = new int[3];
            
            foreach (var key in nums)
                count[key]++;
            
            int start = 0;
            for (int i = 0; i < 3; ++i)
            {
                int amount = count[i];
                for (int j = 0; j < amount; ++j)
                    nums[start + j] = i;
                start += amount;
            }
        }
    }
    

Log in to reply
 

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