2
2
3
3
## 题目
4
4
5
- Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length.
5
+ Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new
6
+ length.
6
7
7
- Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
8
+ Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra
9
+ memory.
8
10
9
11
** Example 1** :
10
12
@@ -34,7 +36,8 @@ It doesn't matter what values are set beyond the returned length.
34
36
35
37
Confused why the returned value is an integer but your answer is an array?
36
38
37
- Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
39
+ Note that the input array is passed in by reference, which means modification to the input array will be known to the
40
+ caller as well.
38
41
39
42
Internally you can think of this:
40
43
@@ -69,10 +72,10 @@ package leetcode
69
72
70
73
func removeDuplicates (nums []int ) int {
71
74
slow := 0
72
- for i , v := range nums {
73
- if i < 2 || nums[slow-2 ] != v{
75
+ for fast , v := range nums {
76
+ if fast < 2 || nums[slow-2 ] != v {
74
77
nums[slow] = v
75
- slow ++
78
+ slow++
76
79
}
77
80
}
78
81
return slow
@@ -81,7 +84,6 @@ func removeDuplicates(nums []int) int {
81
84
82
85
```
83
86
84
-
85
87
----------------------------------------------
86
88
<div style =" display : flex ;justify-content : space-between ;align-items : center ;" >
87
89
<p ><a href =" https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0079.Word-Search/ " >⬅️上一页</a ></p >
0 commit comments