80.go 424 B

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