Skip to content

Commit f880b5d

Browse files
aQuaaQua
aQua
authored and
aQua
committed
80 added
1 parent 41ade7f commit f880b5d

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# [80. Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/)
2+
3+
## 题目
4+
Follow up for "Remove Duplicates":
5+
What if duplicates are allowed at most twice?
6+
7+
8+
For example,
9+
Given sorted array nums = `[1,1,1,2,2,3]`,
10+
11+
12+
Your function should return length = `5`, with the first five elements of nums being `1`, `1`, `2`, `2` and `3`. It doesn't matter what you leave beyond the new length.
13+
14+
## 解题思路
15+
16+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package Problem0080
2+
3+
func removeDuplicates(nums []int) int {
4+
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package Problem0080
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type question struct {
11+
para
12+
ans
13+
}
14+
15+
// para 是参数
16+
type para struct {
17+
nums []int
18+
}
19+
20+
// ans 是答案
21+
type ans struct {
22+
one int
23+
first []int
24+
}
25+
26+
func Test_Problem0080(t *testing.T) {
27+
ast := assert.New(t)
28+
29+
qs := []question{
30+
31+
question{
32+
para{
33+
[]int{1, 1, 1, 2, 2, 3},
34+
},
35+
ans{
36+
5,
37+
[]int{1, 1, 2, 2, 3},
38+
},
39+
},
40+
41+
// 如需多个测试,可以复制上方元素。
42+
}
43+
44+
for _, q := range qs {
45+
a, p := q.ans, q.para
46+
fmt.Printf("~~%v~~\n", p)
47+
48+
end := removeDuplicates(p.nums)
49+
ast.Equal(a.one, end, "输入:%v", p)
50+
ast.Equal(a.first, p.nums[:end], "输入:%v", p)
51+
}
52+
}

0 commit comments

Comments
 (0)