80.go 437 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func removeDuplicates(nums []int) int {
  6. n := len(nums)
  7. if n < 3 {
  8. return n
  9. }
  10. res, cnt := 1, 1
  11. for i := 1; i < n; i++ {
  12. if nums[i] == nums[i-1] {
  13. cnt++
  14. } else {
  15. cnt = 1
  16. }
  17. if cnt <= 2 {
  18. nums[res] = nums[i]
  19. res++
  20. }
  21. }
  22. return res
  23. }
  24. func main() {
  25. a1 := []int{0, 1, 1, 1, 2, 3, 3, 3}
  26. fmt.Println(removeDuplicates(a1))
  27. fmt.Println(a1)
  28. fmt.Println(removeDuplicates([]int{}))
  29. }