75.go 419 B

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