Skip to content

Commit 7a3fe1f

Browse files
committed
add 80 solution explain
1 parent 38875b1 commit 7a3fe1f

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

leetcode/0080.Remove-Duplicates-from-Sorted-Array-II/80. Remove Duplicates from Sorted Array II.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package leetcode
22

33
func removeDuplicates(nums []int) int {
44
slow := 0
5-
for i, v := range nums {
6-
if i < 2 || nums[slow-2] != v {
5+
for fast, v := range nums {
6+
if fast < 2 || nums[slow-2] != v {
77
nums[slow] = v
88
slow++
99
}

leetcode/0080.Remove-Duplicates-from-Sorted-Array-II/README.md

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

5252
## 解题思路
5353

54-
这道题和第 26 题很像。是第 26 题的加强版。这道题和第 283 题,第 27 题基本一致,283 题是删除 0,27 题是删除指定元素,这一题是删除重复元素,实质是一样的
55-
56-
这里数组的删除并不是真的删除,只是将删除的元素移动到数组后面的空间内,然后返回数组实际剩余的元素个数,OJ 最终判断题目的时候会读取数组剩余个数的元素进行输出。
54+
问题提示有序数组,一般最容易想到使用双指针的解法,双指针的关键点:移动两个指针的条件
55+
在该题中移动的条件:快指针从头遍历数组,慢指针指向修改后的数组的末端,当慢指针指向倒数第二个数与快指针指向的数不相等时,才移动慢指针,同时赋值慢指针
56+
处理边界条件:当数组小于两个元素时,不做处理

website/content/ChapterFour/0001~0099/0080.Remove-Duplicates-from-Sorted-Array-II.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
## 题目
44

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.
67

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.
810

911
**Example 1**:
1012

@@ -34,7 +36,8 @@ It doesn't matter what values are set beyond the returned length.
3436

3537
Confused why the returned value is an integer but your answer is an array?
3638

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.
3841

3942
Internally you can think of this:
4043

@@ -69,10 +72,10 @@ package leetcode
6972

7073
func removeDuplicates(nums []int) int {
7174
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 {
7477
nums[slow] = v
75-
slow ++
78+
slow++
7679
}
7780
}
7881
return slow
@@ -81,7 +84,6 @@ func removeDuplicates(nums []int) int {
8184

8285
```
8386

84-
8587
----------------------------------------------
8688
<div style="display: flex;justify-content: space-between;align-items: center;">
8789
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0079.Word-Search/">⬅️上一页</a></p>

0 commit comments

Comments
 (0)