Skip to content

Commit 46c8b45

Browse files
committed
[E:49/531, M:52/969, H:6/386] add No: 26 Remove Duplicates from Sorted Array
1 parent 2c701b6 commit 46c8b45

File tree

13 files changed

+403
-1
lines changed

13 files changed

+403
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## [删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)
2+
3+
给定一个排序数组,你需要在 **[原地](http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)** 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
4+
5+
不要使用额外的数组空间,你必须在 **[原地] (https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)修改输入数组** 并在使用 O(1) 额外空间的条件下完成。
6+
7+
 
8+
9+
**示例 1:**
10+
11+
`给定数组 _nums_ = **[1,1,2]**,
12+
13+
函数应该返回新的长度 **2**, 并且原数组 _nums_ 的前两个元素被修改为 **`1`**, **`2`**
14+
15+
你不需要考虑数组中超出新长度后面的元素。`
16+
17+
**示例 2:**
18+
19+
`给定 _nums_ = **[0,0,1,1,1,2,2,3,3,4]**,
20+
21+
函数应该返回新的长度 **5**, 并且原数组 _nums_ 的前五个元素被修改为 **`0`**, **`1`**, **`2`**, **`3`**, **`4`**
22+
23+
你不需要考虑数组中超出新长度后面的元素。
24+
`
25+
26+
 
27+
28+
**说明:**
29+
30+
为什么返回数值是整数,但输出的答案是数组呢?
31+
32+
请注意,输入数组是以**「引用」**方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
33+
34+
你可以想象内部操作如下:
35+
36+
`// **nums** 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
37+
int len = removeDuplicates(nums);
38+
39+
// 在函数里修改输入数组对于调用者是可见的。
40+
// 根据你的函数返回的长度, 它会打印出数组中**该长度范围内**的所有元素。
41+
for (int i = 0; i < len; i++) {
42+
    print(nums[i]);
43+
}
44+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/26/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[1,1,2]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want int
24+
}{
25+
{
26+
name: "test-[1,1,2]",
27+
input: []int{1, 1, 2},
28+
want: 2,
29+
},
30+
{
31+
name: "test-[0,0,1,1,1,2,2,3,3,4]",
32+
input: []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4},
33+
want: 5,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
// solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(nums []int) int {
4+
return removeDuplicates(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func removeDuplicates(nums []int) int {
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(nums []int) int {
4+
return removeDuplicates(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func removeDuplicates(nums []int) int {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## [删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)
2+
3+
给定一个排序数组,你需要在 **[原地](http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)** 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
4+
5+
不要使用额外的数组空间,你必须在 **[原地] (https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)修改输入数组** 并在使用 O(1) 额外空间的条件下完成。
6+
7+
 
8+
9+
**示例 1:**
10+
11+
`给定数组 _nums_ = **[1,1,2]**,
12+
13+
函数应该返回新的长度 **2**, 并且原数组 _nums_ 的前两个元素被修改为 **`1`**, **`2`**
14+
15+
你不需要考虑数组中超出新长度后面的元素。`
16+
17+
**示例 2:**
18+
19+
`给定 _nums_ = **[0,0,1,1,1,2,2,3,3,4]**,
20+
21+
函数应该返回新的长度 **5**, 并且原数组 _nums_ 的前五个元素被修改为 **`0`**, **`1`**, **`2`**, **`3`**, **`4`**
22+
23+
你不需要考虑数组中超出新长度后面的元素。
24+
`
25+
26+
 
27+
28+
**说明:**
29+
30+
为什么返回数值是整数,但输出的答案是数组呢?
31+
32+
请注意,输入数组是以**「引用」**方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
33+
34+
你可以想象内部操作如下:
35+
36+
`// **nums** 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
37+
int len = removeDuplicates(nums);
38+
39+
// 在函数里修改输入数组对于调用者是可见的。
40+
// 根据你的函数返回的长度, 它会打印出数组中**该长度范围内**的所有元素。
41+
for (int i = 0; i < len; i++) {
42+
    print(nums[i]);
43+
}
44+
`
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"reflect"
7+
"time"
8+
9+
"github.com/gladmo/leetcode/leet"
10+
"github.com/gladmo/leetcode/questions/serial/简单/26/golang/solution"
11+
)
12+
13+
func main() {
14+
/*
15+
16+
[1,1,2]
17+
18+
*/
19+
20+
tests := []struct {
21+
name string
22+
input []int
23+
want int
24+
}{
25+
{
26+
name: "test-[1,1,2]",
27+
input: []int{1, 1, 2},
28+
want: 2,
29+
},
30+
{
31+
name: "test-[0,0,1,1,1,2,2,3,3,4]",
32+
input: []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4},
33+
want: 5,
34+
},
35+
}
36+
37+
testLog := leet.NewTestLog(len(tests))
38+
defer testLog.Render()
39+
40+
timeoutDuration := time.Second * 2
41+
42+
for idx, test := range tests {
43+
// 超时检测
44+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
45+
// solution.Export(test.input)
46+
cancel()
47+
})
48+
49+
if timeout {
50+
testLog.Fail(idx+1, test.name, "timeout")
51+
continue
52+
}
53+
54+
got := solution.Export(test.input)
55+
if !reflect.DeepEqual(test.want, got) {
56+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
57+
continue
58+
}
59+
60+
testLog.Pass(idx+1, test.name)
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(nums []int) int {
4+
return removeDuplicates(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func removeDuplicates(nums []int) int {
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package solution
2+
3+
func Export(nums []int) int {
4+
return removeDuplicates(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func removeDuplicates(nums []int) int {
13+
14+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## [删除排序数组中的重复项](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/)
2+
3+
给定一个排序数组,你需要在 **[原地](http://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)** 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。
4+
5+
不要使用额外的数组空间,你必须在 **[原地] (https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)修改输入数组** 并在使用 O(1) 额外空间的条件下完成。
6+
7+
 
8+
9+
**示例 1:**
10+
11+
`给定数组 _nums_ = **[1,1,2]**,
12+
13+
函数应该返回新的长度 **2**, 并且原数组 _nums_ 的前两个元素被修改为 **`1`**, **`2`**
14+
15+
你不需要考虑数组中超出新长度后面的元素。`
16+
17+
**示例 2:**
18+
19+
`给定 _nums_ = **[0,0,1,1,1,2,2,3,3,4]**,
20+
21+
函数应该返回新的长度 **5**, 并且原数组 _nums_ 的前五个元素被修改为 **`0`**, **`1`**, **`2`**, **`3`**, **`4`**
22+
23+
你不需要考虑数组中超出新长度后面的元素。
24+
`
25+
26+
 
27+
28+
**说明:**
29+
30+
为什么返回数值是整数,但输出的答案是数组呢?
31+
32+
请注意,输入数组是以**「引用」**方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
33+
34+
你可以想象内部操作如下:
35+
36+
`// **nums** 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
37+
int len = removeDuplicates(nums);
38+
39+
// 在函数里修改输入数组对于调用者是可见的。
40+
// 根据你的函数返回的长度, 它会打印出数组中**该长度范围内**的所有元素。
41+
for (int i = 0; i < len; i++) {
42+
    print(nums[i]);
43+
}
44+
`

0 commit comments

Comments
 (0)