C# Solution - Ugly version


  • 0
        public void SortColors(int[] nums) {
            int redCount = 0, whiteCount = 0, blueCount = 0, index = 0;
            
            foreach (var item in nums)
                if (item == 0)
                    redCount++;
                else if (item == 1)
                    whiteCount++;
                else
                    blueCount++;
            
            while(redCount-- > 0)
                nums[index++] = 0;
            
            while(whiteCount-- > 0)
                nums[index++] = 1;
                
            while(blueCount-- > 0)
                nums[index++] = 2;
        }

Log in to reply
 

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