1234567891011121314151617181920212223242526272829303132 |
- package main
- import (
- "fmt"
- )
- func removeDuplicates(nums []int) int {
- n := len(nums)
- if n < 3 {
- return n
- }
- res, cnt := 1, 1
- for i := 1; i < n; i++ {
- if nums[i] == nums[i-1] {
- cnt++
- } else {
- cnt = 1
- }
- if cnt <= 2 {
- nums[res] = nums[i]
- res++
- }
- }
- return res
- }
- func main() {
- a1 := []int{0, 1, 1, 1, 2, 3, 3, 3}
- fmt.Println(removeDuplicates(a1))
- fmt.Println(a1)
- fmt.Println(removeDuplicates([]int{}))
- }
|