75.go 406 B

12345678910111213141516171819202122
  1. package main
  2. // ?? check the best solution the next time
  3. func sortColors(nums []int) {
  4. mCnt := [3]int{}
  5. for _, v := range nums {
  6. mCnt[v]++
  7. }
  8. for idx, color := 0, 0; color < 3; color++ {
  9. cnt := mCnt[color]
  10. for i := 0; i < cnt; idx, i = idx+1, i+1 {
  11. nums[idx] = color
  12. }
  13. }
  14. }
  15. /* func main() {
  16. c1 := []int{0, 1, 1, 2, 0, 0, 1, 2, 1}
  17. sortColors(c1)
  18. fmt.Println(c1)
  19. sortColors([]int{})
  20. } */