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

Commit 32cda3e

Browse files
aQuaaQua
aQua
authored and
aQua
committed
154 accepted
1 parent 3206c5f commit 32cda3e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# [154. Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/)
2+
3+
## 题目
4+
Follow up for "Find Minimum in Rotated Sorted Array":
5+
What if duplicates are allowed?
6+
7+
Would this affect the run-time complexity? How and why?
8+
9+
10+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
11+
12+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
13+
14+
Find the minimum element.
15+
16+
The array may contain duplicates.
17+
18+
## 解题思路
19+
20+
见程序注释
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package Problem0154
2+
3+
func findMin(a []int) int {
4+
Len := len(a)
5+
6+
i := 1
7+
for i < Len && a[i-1] <= a[i] {
8+
i++
9+
}
10+
11+
return a[i%Len]
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package Problem0154
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+
}
24+
25+
func Test_Problem0154(t *testing.T) {
26+
ast := assert.New(t)
27+
28+
qs := []question{
29+
30+
question{
31+
para{
32+
[]int{4, 4, 5, 5, 6, 6, 7, 7, 0, 0, 1, 1, 2, 2},
33+
},
34+
ans{
35+
0,
36+
},
37+
},
38+
39+
question{
40+
para{
41+
[]int{1,1},
42+
},
43+
ans{
44+
1,
45+
},
46+
},
47+
48+
question{
49+
para{
50+
[]int{1, 2},
51+
},
52+
ans{
53+
1,
54+
},
55+
},
56+
57+
// 如需多个测试,可以复制上方元素。
58+
}
59+
60+
for _, q := range qs {
61+
a, p := q.ans, q.para
62+
fmt.Printf("~~%v~~\n", p)
63+
64+
ast.Equal(a.one, findMin(p.nums), "输入:%v", p)
65+
}
66+
}

0 commit comments

Comments
 (0)