Skip to content

Commit 4ae39dc

Browse files
Update 80. 删除有序数组中的重复项 II.md
1 parent 769ecf7 commit 4ae39dc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

Two Pointers/80. 删除有序数组中的重复项 II.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ for (int i = 0; i < len; i++) {
5151

5252
---
5353

54-
快慢指针(或者滑动窗口)
54+
快慢指针:
5555

5656
慢指针记录被替换的下标,快指针记录待检查的下标。
5757

58-
或者是长度为 3 的滑动窗口,该窗口中不能包含相等的元素。
59-
6058
```Java
59+
//Java
6160
class Solution {
6261
public int removeDuplicates(int[] nums) {
6362
int n = nums.length;
@@ -73,4 +72,24 @@ class Solution {
7372
return slow;
7473
}
7574
}
75+
```
76+
77+
78+
```go
79+
//Go
80+
func removeDuplicates(nums []int) int {
81+
n := len(nums)
82+
if n <= 2 {
83+
return n
84+
}
85+
slow, fast := 2, 2
86+
for ;fast < n; {
87+
if nums[slow - 2] != nums[fast] {
88+
nums[slow] = nums[fast]
89+
slow++
90+
}
91+
fast++
92+
}
93+
return slow
94+
}
7695
```

0 commit comments

Comments
 (0)