Skip to content

Commit 315d4fa

Browse files
authored
Merge pull request #139 from NovaHe/fix/91
Fix/91 and 80
2 parents 00b6d1a + 7a3fe1f commit 315d4fa

File tree

6 files changed

+36
-106
lines changed

6 files changed

+36
-106
lines changed
Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
11
package leetcode
22

3-
func removeDuplicates80(nums []int) int {
4-
if len(nums) == 0 {
5-
return 0
6-
}
7-
last, finder := 0, 0
8-
for last < len(nums)-1 {
9-
startFinder := -1
10-
for nums[finder] == nums[last] {
11-
if startFinder == -1 || startFinder > finder {
12-
startFinder = finder
13-
}
14-
if finder == len(nums)-1 {
15-
break
16-
}
17-
finder++
18-
}
19-
if finder-startFinder >= 2 && nums[finder-1] == nums[last] && nums[finder] != nums[last] {
20-
nums[last+1] = nums[finder-1]
21-
nums[last+2] = nums[finder]
22-
last += 2
23-
} else {
24-
nums[last+1] = nums[finder]
25-
last++
26-
}
27-
if finder == len(nums)-1 {
28-
if nums[finder] != nums[last-1] {
29-
nums[last] = nums[finder]
30-
}
31-
return last + 1
3+
func removeDuplicates(nums []int) int {
4+
slow := 0
5+
for fast, v := range nums {
6+
if fast < 2 || nums[slow-2] != v {
7+
nums[slow] = v
8+
slow++
329
}
3310
}
34-
return last + 1
11+
return slow
3512
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_Problem80(t *testing.T) {
6161

6262
for _, q := range qs {
6363
_, p := q.ans80, q.para80
64-
fmt.Printf("【input】:%v 【output】:%v\n", p.one, removeDuplicates80(p.one))
64+
fmt.Printf("【input】:%v 【output】:%v\n", p.one, removeDuplicates(p.one))
6565
}
6666
fmt.Printf("\n\n\n")
6767
}

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+
处理边界条件:当数组小于两个元素时,不做处理
Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
package leetcode
22

3-
import (
4-
"strconv"
5-
)
6-
73
func numDecodings(s string) int {
8-
if len(s) == 0 {
9-
return 0
10-
}
11-
dp := make([]int, len(s)+1)
4+
n := len(s)
5+
dp := make([]int, n+1)
126
dp[0] = 1
13-
if s[:1] == "0" {
14-
dp[1] = 0
15-
} else {
16-
dp[1] = 1
17-
}
18-
for i := 2; i <= len(s); i++ {
19-
lastNum, _ := strconv.Atoi(s[i-1 : i])
20-
if lastNum >= 1 && lastNum <= 9 {
7+
for i := 1; i <= n; i++ {
8+
if s[i-1] != '0' {
219
dp[i] += dp[i-1]
2210
}
23-
lastNum, _ = strconv.Atoi(s[i-2 : i])
24-
if lastNum >= 10 && lastNum <= 26 {
11+
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
2512
dp[i] += dp[i-2]
2613
}
2714
}
28-
return dp[len(s)]
15+
return dp[n]
2916
}

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

Lines changed: 13 additions & 34 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

@@ -67,44 +70,20 @@ for (int i = 0; i < len; i++) {
6770

6871
package leetcode
6972

70-
func removeDuplicates80(nums []int) int {
71-
if len(nums) == 0 {
72-
return 0
73-
}
74-
last, finder := 0, 0
75-
for last < len(nums)-1 {
76-
startFinder := -1
77-
for nums[finder] == nums[last] {
78-
if startFinder == -1 || startFinder > finder {
79-
startFinder = finder
80-
}
81-
if finder == len(nums)-1 {
82-
break
83-
}
84-
finder++
85-
}
86-
if finder-startFinder >= 2 && nums[finder-1] == nums[last] && nums[finder] != nums[last] {
87-
nums[last+1] = nums[finder-1]
88-
nums[last+2] = nums[finder]
89-
last += 2
90-
} else {
91-
nums[last+1] = nums[finder]
92-
last++
93-
}
94-
if finder == len(nums)-1 {
95-
if nums[finder] != nums[last-1] {
96-
nums[last] = nums[finder]
97-
}
98-
return last + 1
73+
func removeDuplicates(nums []int) int {
74+
slow := 0
75+
for fast, v := range nums {
76+
if fast < 2 || nums[slow-2] != v {
77+
nums[slow] = v
78+
slow++
9979
}
10080
}
101-
return last + 1
81+
return slow
10282
}
10383

10484

10585
```
10686

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

website/content/ChapterFour/0001~0099/0091.Decode-Ways.md

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,19 @@ Given a **non-empty** string containing only digits, determine the total numbe
5151

5252
package leetcode
5353

54-
import (
55-
"strconv"
56-
)
57-
5854
func numDecodings(s string) int {
59-
if len(s) == 0 {
60-
return 0
61-
}
62-
dp := make([]int, len(s)+1)
55+
n := len(s)
56+
dp := make([]int, n+1)
6357
dp[0] = 1
64-
if s[:1] == "0" {
65-
dp[1] = 0
66-
} else {
67-
dp[1] = 1
68-
}
69-
for i := 2; i <= len(s); i++ {
70-
lastNum, _ := strconv.Atoi(s[i-1 : i])
71-
if lastNum >= 1 && lastNum <= 9 {
58+
for i := 1; i <= n; i++ {
59+
if s[i-1] != '0' {
7260
dp[i] += dp[i-1]
7361
}
74-
lastNum, _ = strconv.Atoi(s[i-2 : i])
75-
if lastNum >= 10 && lastNum <= 26 {
62+
if i > 1 && s[i-2] != '0' && (s[i-2]-'0')*10+(s[i-1]-'0') <= 26 {
7663
dp[i] += dp[i-2]
7764
}
7865
}
79-
return dp[len(s)]
66+
return dp[n]
8067
}
8168

8269
```

0 commit comments

Comments
 (0)