Skip to content

Commit 39a51aa

Browse files
committed
[E:47/531, M:52/969, H:6/386] add No: 268 Missing Number
1 parent 1a2da52 commit 39a51aa

File tree

17 files changed

+581
-1
lines changed

17 files changed

+581
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## [丢失的数字](https://leetcode-cn.com/problems/missing-number/)
2+
3+
给定一个包含 `[0, n]` 中 `n` 个数的数组 `nums` ,找出 `[0, n]` 这个范围内没有出现在数组中的那个数。
4+
5+
 
6+
7+
**进阶:**
8+
9+
* 你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
10+
11+
 
12+
13+
**示例 1:**
14+
15+
`
16+
**输入:**nums = [3,0,1]
17+
**输出:**2
18+
**解释:**n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
19+
20+
**示例 2:**
21+
22+
`
23+
**输入:**nums = [0,1]
24+
**输出:**2
25+
**解释:**n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
26+
27+
**示例 3:**
28+
29+
`
30+
**输入:**nums = [9,6,4,2,3,5,7,0,1]
31+
**输出:**8
32+
**解释:**n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。`
33+
34+
**示例 4:**
35+
36+
`
37+
**输入:**nums = [0]
38+
**输出:**1
39+
**解释:**n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。`
40+
41+
 
42+
43+
**提示:**
44+
45+
* `n == nums.length`
46+
* `1
47+
4
48+
`
49+
* `0 <= nums[i] <= n`
50+
* `nums` 中的所有数字都 **独一无二**
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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/简单/268/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input []int
18+
want int
19+
}{
20+
{
21+
name: "test-3,0,1",
22+
input: []int{3, 0, 1},
23+
want: 2,
24+
},
25+
{
26+
name: "test-0,1",
27+
input: []int{0, 1},
28+
want: 2,
29+
},
30+
{
31+
name: "test-9,6,4,2,3,5,7,0,1",
32+
input: []int{9, 6, 4, 2, 3, 5, 7, 0, 1},
33+
want: 8,
34+
},
35+
{
36+
name: "test-0",
37+
input: []int{0},
38+
want: 1,
39+
},
40+
}
41+
42+
testLog := leet.NewTestLog(len(tests))
43+
defer testLog.Render()
44+
45+
timeoutDuration := time.Second * 2
46+
47+
for idx, test := range tests {
48+
// 超时检测
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
solution.Export(test.input)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
got := solution.Export(test.input)
60+
if !reflect.DeepEqual(test.want, got) {
61+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
62+
continue
63+
}
64+
65+
testLog.Pass(idx+1, test.name)
66+
}
67+
}
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 missingNumber(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func missingNumber(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 missingNumber(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func missingNumber(nums []int) int {
13+
14+
}

questions/store.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## [丢失的数字](https://leetcode-cn.com/problems/missing-number/)
2+
3+
给定一个包含 `[0, n]` 中 `n` 个数的数组 `nums` ,找出 `[0, n]` 这个范围内没有出现在数组中的那个数。
4+
5+
 
6+
7+
**进阶:**
8+
9+
* 你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
10+
11+
 
12+
13+
**示例 1:**
14+
15+
`
16+
**输入:**nums = [3,0,1]
17+
**输出:**2
18+
**解释:**n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
19+
20+
**示例 2:**
21+
22+
`
23+
**输入:**nums = [0,1]
24+
**输出:**2
25+
**解释:**n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
26+
27+
**示例 3:**
28+
29+
`
30+
**输入:**nums = [9,6,4,2,3,5,7,0,1]
31+
**输出:**8
32+
**解释:**n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。`
33+
34+
**示例 4:**
35+
36+
`
37+
**输入:**nums = [0]
38+
**输出:**1
39+
**解释:**n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。`
40+
41+
 
42+
43+
**提示:**
44+
45+
* `n == nums.length`
46+
* `1
47+
4
48+
`
49+
* `0 <= nums[i] <= n`
50+
* `nums` 中的所有数字都 **独一无二**
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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/简单/268/golang/solution"
11+
)
12+
13+
func main() {
14+
15+
tests := []struct {
16+
name string
17+
input []int
18+
want int
19+
}{
20+
{
21+
name: "test-3,0,1",
22+
input: []int{3, 0, 1},
23+
want: 2,
24+
},
25+
{
26+
name: "test-0,1",
27+
input: []int{0, 1},
28+
want: 2,
29+
},
30+
{
31+
name: "test-9,6,4,2,3,5,7,0,1",
32+
input: []int{9, 6, 4, 2, 3, 5, 7, 0, 1},
33+
want: 8,
34+
},
35+
{
36+
name: "test-0",
37+
input: []int{0},
38+
want: 1,
39+
},
40+
}
41+
42+
testLog := leet.NewTestLog(len(tests))
43+
defer testLog.Render()
44+
45+
timeoutDuration := time.Second * 2
46+
47+
for idx, test := range tests {
48+
// 超时检测
49+
timeout := leet.Timeout(timeoutDuration, func(ctx context.Context, cancel context.CancelFunc) {
50+
solution.Export(test.input)
51+
cancel()
52+
})
53+
54+
if timeout {
55+
testLog.Fail(idx+1, test.name, "timeout")
56+
continue
57+
}
58+
59+
got := solution.Export(test.input)
60+
if !reflect.DeepEqual(test.want, got) {
61+
testLog.Fail(idx+1, test.name, fmt.Sprintf("want: %v, got %v.", test.want, got))
62+
continue
63+
}
64+
65+
testLog.Pass(idx+1, test.name)
66+
}
67+
}
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 missingNumber(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func missingNumber(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 missingNumber(nums)
5+
}
6+
7+
/****************************************************/
8+
/******** 以下为 Leetcode 示例部分(提交PR请还原) *******/
9+
/******** 使用 (./leetcode clear) 初始化所有问题 *******/
10+
/****************************************************/
11+
12+
func missingNumber(nums []int) int {
13+
14+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## [丢失的数字](https://leetcode-cn.com/problems/missing-number/)
2+
3+
给定一个包含 `[0, n]` 中 `n` 个数的数组 `nums` ,找出 `[0, n]` 这个范围内没有出现在数组中的那个数。
4+
5+
 
6+
7+
**进阶:**
8+
9+
* 你能否实现线性时间复杂度、仅使用额外常数空间的算法解决此问题?
10+
11+
 
12+
13+
**示例 1:**
14+
15+
`
16+
**输入:**nums = [3,0,1]
17+
**输出:**2
18+
**解释:**n = 3,因为有 3 个数字,所以所有的数字都在范围 [0,3] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
19+
20+
**示例 2:**
21+
22+
`
23+
**输入:**nums = [0,1]
24+
**输出:**2
25+
**解释:**n = 2,因为有 2 个数字,所以所有的数字都在范围 [0,2] 内。2 是丢失的数字,因为它没有出现在 nums 中。`
26+
27+
**示例 3:**
28+
29+
`
30+
**输入:**nums = [9,6,4,2,3,5,7,0,1]
31+
**输出:**8
32+
**解释:**n = 9,因为有 9 个数字,所以所有的数字都在范围 [0,9] 内。8 是丢失的数字,因为它没有出现在 nums 中。`
33+
34+
**示例 4:**
35+
36+
`
37+
**输入:**nums = [0]
38+
**输出:**1
39+
**解释:**n = 1,因为有 1 个数字,所以所有的数字都在范围 [0,1] 内。1 是丢失的数字,因为它没有出现在 nums 中。`
40+
41+
 
42+
43+
**提示:**
44+
45+
* `n == nums.length`
46+
* `1
47+
4
48+
`
49+
* `0 <= nums[i] <= n`
50+
* `nums` 中的所有数字都 **独一无二**

0 commit comments

Comments
 (0)