Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2d1ebc7

Browse files
aQuaaQua
aQua
authored and
aQua
committed
26 翻新了题解。
1 parent e03f1fb commit 2d1ebc7

File tree

4 files changed

+88
-74
lines changed

4 files changed

+88
-74
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
# [26. Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)
22

33
## 题目
4-
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
54

6-
Do not allocate extra space for another array, you must do this in place with constant memory.
5+
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
76

8-
For example,
9-
Given input array nums = [1,1,2],
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.
108

11-
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
9+
Example 1:
1210

13-
## 解题思路
14-
题目要求原地删除重复的元素。并返回没有重复元素的切片的长度。
11+
```text
12+
Given nums = [1,1,2],
13+
14+
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
15+
16+
It doesn't matter what you leave beyond the returned length.
17+
```
18+
19+
Example 2:
20+
21+
```text
22+
Given nums = [0,0,1,1,1,2,2,3,3,4],
23+
24+
Your function should return length = 5, with the first five elements of nums being modified to0, 1, 2, 3, and4 respectively.
25+
26+
It doesn't matter what values are set beyondthe returned length.
27+
```
1528

16-
## 总结
29+
Clarification:
1730

31+
Confused why the returned value is an integer but your answer is an array?
32+
33+
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.
34+
35+
Internally you can think of this:
36+
37+
```text
38+
// nums is passed in by reference. (i.e., without making a copy)
39+
int len = removeDuplicates(nums);
40+
41+
// any modification to nums in your function would be known by the caller.
42+
// using the length returned by your function, it prints the first len elements.
43+
for (int i = 0; i < len; i++) {
44+
print(nums[i]);
45+
}
46+
```
47+
48+
## 解题思路
1849

50+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
11
package problem0026
22

3-
func removeDuplicates(nums []int) int {
4-
if len(nums) <= 1 {
5-
return len(nums)
6-
}
7-
8-
res := 1
9-
i := 1
10-
for ; i < len(nums); i++ {
11-
if nums[i] == nums[i-1] {
3+
func removeDuplicates(a []int) int {
4+
left, right, size := 0, 1, len(a)
5+
for ; right < size; right++ {
6+
if a[left] == a[right] {
127
continue
138
}
14-
if res != i {
15-
nums[res] = nums[i]
16-
}
17-
18-
res++
9+
left++
10+
a[left], a[right] = a[right], a[left]
1911
}
20-
21-
return res
12+
return left + 1
2213
}

Algorithms/0026.remove-duplicates-from-sorted-array/remove-duplicates-from-sorted-array_test.go

+27-36
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,38 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10-
type question struct {
11-
para
12-
ans
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
nums []int
13+
ans int
14+
}{
15+
16+
{
17+
[]int{1, 1, 2},
18+
2,
19+
},
20+
21+
{
22+
[]int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4},
23+
5,
24+
},
25+
26+
// 可以有多个 testcase
1327
}
1428

15-
// para 是参数
16-
// one 代表第一个参数
17-
type para struct {
18-
one []int
19-
}
20-
21-
// ans 是答案
22-
// one 代表第一个答案
23-
type ans struct {
24-
one int
25-
two []int
26-
}
27-
28-
func Test_Problem0026(t *testing.T) {
29+
func Test_removeDuplicates(t *testing.T) {
2930
ast := assert.New(t)
3031

31-
qs := []question{
32-
33-
question{
34-
para{[]int{1, 2, 2, 3, 3, 4, 5}},
35-
ans{5, []int{1, 2, 3, 4, 5}},
36-
},
37-
38-
question{
39-
para{[]int{1}},
40-
ans{1, []int{1}},
41-
},
42-
43-
// 如需多个测试,可以复制上方元素。
32+
for _, tc := range tcs {
33+
fmt.Printf("~~%v~~\n", tc)
34+
ast.Equal(tc.ans, removeDuplicates(tc.nums), "输入:%v", tc)
4435
}
36+
}
4537

46-
for _, q := range qs {
47-
a, p := q.ans, q.para
48-
fmt.Printf("~~%v~~\n", p)
49-
l := removeDuplicates(p.one)
50-
ast.Equal(a.one, l, "输入:%v", p)
51-
ast.Equal(a.two, p.one[:l])
38+
func Benchmark_removeDuplicates(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
for _, tc := range tcs {
41+
removeDuplicates(tc.nums)
42+
}
5243
}
5344
}

leetcode.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 1089,
4-
"Updated": "2018-05-21T17:08:00.86638057+08:00",
3+
"Ranking": 1070,
4+
"Updated": "2018-05-22T13:25:37.766569899+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 169,
@@ -325,7 +325,7 @@
325325
"ID": 25,
326326
"Title": "Reverse Nodes in k-Group",
327327
"TitleSlug": "reverse-nodes-in-k-group",
328-
"PassRate": "31%",
328+
"PassRate": "32%",
329329
"Difficulty": "Hard",
330330
"IsAccepted": true,
331331
"IsPaid": false,
@@ -4585,7 +4585,7 @@
45854585
"ID": 380,
45864586
"Title": "Insert Delete GetRandom O(1)",
45874587
"TitleSlug": "insert-delete-getrandom-o1",
4588-
"PassRate": "40%",
4588+
"PassRate": "39%",
45894589
"Difficulty": "Medium",
45904590
"IsAccepted": true,
45914591
"IsPaid": false,
@@ -8125,7 +8125,7 @@
81258125
"ID": 675,
81268126
"Title": "Cut Off Trees for Golf Event",
81278127
"TitleSlug": "cut-off-trees-for-golf-event",
8128-
"PassRate": "27%",
8128+
"PassRate": "26%",
81298129
"Difficulty": "Hard",
81308130
"IsAccepted": true,
81318131
"IsPaid": false,
@@ -8137,7 +8137,7 @@
81378137
"ID": 676,
81388138
"Title": "Implement Magic Dictionary",
81398139
"TitleSlug": "implement-magic-dictionary",
8140-
"PassRate": "49%",
8140+
"PassRate": "48%",
81418141
"Difficulty": "Medium",
81428142
"IsAccepted": true,
81438143
"IsPaid": false,
@@ -8233,7 +8233,7 @@
82338233
"ID": 684,
82348234
"Title": "Redundant Connection",
82358235
"TitleSlug": "redundant-connection",
8236-
"PassRate": "43%",
8236+
"PassRate": "44%",
82378237
"Difficulty": "Medium",
82388238
"IsAccepted": true,
82398239
"IsPaid": false,
@@ -8725,7 +8725,7 @@
87258725
"ID": 725,
87268726
"Title": "Split Linked List in Parts",
87278727
"TitleSlug": "split-linked-list-in-parts",
8728-
"PassRate": "47%",
8728+
"PassRate": "46%",
87298729
"Difficulty": "Medium",
87308730
"IsAccepted": true,
87318731
"IsPaid": false,
@@ -9409,7 +9409,7 @@
94099409
"ID": 782,
94109410
"Title": "Transform to Chessboard",
94119411
"TitleSlug": "transform-to-chessboard",
9412-
"PassRate": "36%",
9412+
"PassRate": "37%",
94139413
"Difficulty": "Hard",
94149414
"IsAccepted": true,
94159415
"IsPaid": false,
@@ -9493,7 +9493,7 @@
94939493
"ID": 789,
94949494
"Title": "Escape The Ghosts",
94959495
"TitleSlug": "escape-the-ghosts",
9496-
"PassRate": "49%",
9496+
"PassRate": "50%",
94979497
"Difficulty": "Medium",
94989498
"IsAccepted": true,
94999499
"IsPaid": false,
@@ -9541,7 +9541,7 @@
95419541
"ID": 793,
95429542
"Title": "Preimage Size of Factorial Zeroes Function",
95439543
"TitleSlug": "preimage-size-of-factorial-zeroes-function",
9544-
"PassRate": "43%",
9544+
"PassRate": "42%",
95459545
"Difficulty": "Hard",
95469546
"IsAccepted": true,
95479547
"IsPaid": false,
@@ -9637,7 +9637,7 @@
96379637
"ID": 801,
96389638
"Title": "Minimum Swaps To Make Sequences Increasing",
96399639
"TitleSlug": "minimum-swaps-to-make-sequences-increasing",
9640-
"PassRate": "26%",
9640+
"PassRate": "27%",
96419641
"Difficulty": "Medium",
96429642
"IsAccepted": true,
96439643
"IsPaid": false,
@@ -10069,7 +10069,7 @@
1006910069
"ID": 837,
1007010070
"Title": "New 21 Game",
1007110071
"TitleSlug": "new-21-game",
10072-
"PassRate": "21%",
10072+
"PassRate": "23%",
1007310073
"Difficulty": "Medium",
1007410074
"IsAccepted": false,
1007510075
"IsPaid": false,
@@ -10081,7 +10081,7 @@
1008110081
"ID": 838,
1008210082
"Title": "Push Dominoes",
1008310083
"TitleSlug": "push-dominoes",
10084-
"PassRate": "37%",
10084+
"PassRate": "38%",
1008510085
"Difficulty": "Medium",
1008610086
"IsAccepted": false,
1008710087
"IsPaid": false,

0 commit comments

Comments
 (0)