Skip to content

Commit d9774d5

Browse files
aQuaaQua
aQua
authored and
aQua
committed
adding Problem 33
accepted
1 parent 2274101 commit d9774d5

File tree

3 files changed

+132
-7
lines changed

3 files changed

+132
-7
lines changed

Algorithms/0033.search-in-rotated-sorted-array/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)
22

33
## 题目
4+
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
45

6+
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
7+
8+
You are given a target value to search. If found in the array return its index, otherwise return -1.
9+
10+
You may assume no duplicate exists in the array.
511

612
## 解题思路
713

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
11
package Problem0033
22

3+
func search(nums []int, target int) int {
4+
if len(nums) == 0 {
5+
return -1
6+
}
7+
8+
if nums[len(nums)-1] > nums[0] {
9+
return searchInt(nums, target, 0)
10+
}
11+
12+
base := searchMax(nums) + 1
13+
res := searchInt(nums[:base], target, 0)
14+
if res != -1 {
15+
return res
16+
}
17+
res = searchInt(nums[base:], target, base)
18+
if res != -1 {
19+
return res
20+
}
21+
22+
return -1
23+
}
24+
25+
func searchInt(nums []int, target, base int) (index int) {
26+
lo, hi := 0, len(nums)-1
27+
for lo <= hi {
28+
index = (lo + hi) / 2
29+
switch {
30+
case nums[index] > target:
31+
hi = index - 1
32+
case nums[index] < target:
33+
lo = index + 1
34+
default:
35+
return base + index
36+
}
37+
}
38+
return -1
39+
}
40+
41+
func searchMax(nums []int) (index int) {
42+
i, j := 0, len(nums)-1
43+
44+
for {
45+
index = (i + j) / 2
46+
if index == 0 || index == len(nums)-1 {
47+
return
48+
}
49+
if index > 0 && index < len(nums)-1 && nums[index] > nums[index-1] && nums[index] > nums[index+1] {
50+
return
51+
}
52+
if nums[index] > nums[i] {
53+
i = index
54+
} else {
55+
j = index
56+
}
57+
}
58+
}
Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0033
22

33
import (
4-
"testing"
54
"fmt"
5+
"testing"
66

77
"github.com/stretchr/testify/assert"
88
)
@@ -15,13 +15,14 @@ type question struct {
1515
// para 是参数
1616
// one 代表第一个参数
1717
type para struct {
18-
one string
18+
one []int
19+
two int
1920
}
2021

2122
// ans 是答案
2223
// one 代表第一个答案
2324
type ans struct {
24-
one string
25+
one int
2526
}
2627

2728
func Test_Problem0033(t *testing.T) {
@@ -30,17 +31,79 @@ func Test_Problem0033(t *testing.T) {
3031
qs := []question{
3132

3233
question{
33-
para{""},
34-
ans{""},
34+
para{[]int{4, 5, 6, 7, 0, 1, 2}, 5},
35+
ans{1},
36+
},
37+
38+
question{
39+
para{[]int{6, 7, 0, 1, 2,3,4,5}, 1},
40+
ans{3},
41+
},
42+
43+
question{
44+
para{[]int{1, 3}, 0},
45+
ans{-1},
46+
},
47+
48+
question{
49+
para{[]int{}, 5},
50+
ans{-1},
51+
},
52+
53+
question{
54+
para{[]int{2}, 5},
55+
ans{-1},
56+
},
57+
58+
question{
59+
para{[]int{2}, 2},
60+
ans{0},
3561
},
36-
62+
3763
// 如需多个测试,可以复制上方元素。
3864
}
3965

4066
for _, q := range qs {
4167
a, p := q.ans, q.para
4268
fmt.Printf("~~%v~~\n", p)
4369

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
70+
ast.Equal(a.one, search(p.one, p.two), "输入:%v", p)
4571
}
4672
}
73+
func Test_searchInt(t *testing.T) {
74+
var actual, expected int
75+
ast := assert.New(t)
76+
77+
actual = searchInt([]int{3, 4}, 4, 0)
78+
expected = 1
79+
ast.Equal(expected, actual)
80+
81+
actual = searchInt([]int{3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}, 4, 0)
82+
expected = 1
83+
ast.Equal(expected, actual)
84+
}
85+
func Test_searchMax(t *testing.T) {
86+
var actual, expected int
87+
ast := assert.New(t)
88+
89+
actual = searchMax([]int{9, 1, 2, 3, 4, 5, 6, 7, 8})
90+
expected = 0
91+
ast.Equal(expected, actual)
92+
93+
actual = searchMax([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0})
94+
expected = 8
95+
ast.Equal(expected, actual)
96+
97+
actual = searchMax([]int{4, 5, 6, 7, 0, 1, 2})
98+
expected = 3
99+
ast.Equal(expected, actual)
100+
101+
actual = searchMax([]int{1, 0})
102+
expected = 0
103+
ast.Equal(expected, actual)
104+
105+
actual = searchMax([]int{0})
106+
expected = 0
107+
ast.Equal(expected, actual)
108+
109+
}

0 commit comments

Comments
 (0)