classSolution: defsortColors(self, nums): """ :type nums: List[int] :rtype: void Do not return anything, modify nums in-place instead. """ # 5 star, count = [0] * 3 for i in nums: count[i] += 1 index = 0 for i in range(3): for j in range(count[i]): nums[index] = i index += 1
Go:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcsortColors(nums []int) { count := make([]int, 3) for _, num := range nums { count[num] += 1 } index := 0 for idx, _ := range count { for count[idx] > 0 { nums[index] = idx index++ count[idx] -= 1 } }