Skip to content

Commit 5a8579f

Browse files
aQuaaQua
aQua
authored and
aQua
committed
finish Problem 34
1 parent c77c370 commit 5a8579f

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

Algorithms/0034.search-for-a-range/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
# [34. Search for a Range](https://leetcode.com/problems/search-for-a-range/)
22

33
## 题目
4+
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
45

6+
Your algorithm's runtime complexity must be in the order of O(log n).
57

6-
## 解题思路
8+
If the target is not found in the array, return [-1, -1].
9+
10+
For example,
11+
Given [5, 7, 7, 8, 8, 10] and target value 8,
12+
return [3, 4].
713

14+
## 解题思路
15+
见注释
816

917
## 总结
1018

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,52 @@
11
package Problem0034
22

3+
func searchRange(nums []int, target int) []int {
4+
// 查看target是否存在与nums中
5+
index := search(nums, target)
6+
if index == -1 {
7+
return []int{-1, -1}
8+
}
9+
10+
// 利用二分法,查找第一个target
11+
first := index
12+
for {
13+
f := search(nums[:first], target)
14+
if f == -1 {
15+
break
16+
}
17+
first = f
18+
}
19+
20+
// 利用二分法,查找最后一个target
21+
last := index
22+
for {
23+
l := search(nums[last+1:], target)
24+
if l == -1 {
25+
break
26+
}
27+
// 注意这里与查找first的不同
28+
last += l + 1
29+
}
30+
31+
return []int{first, last}
32+
}
33+
34+
// 二分查找法
35+
func search(nums []int, target int) int {
36+
low, high := 0, len(nums)-1
37+
var median int
38+
39+
for low <= high {
40+
median = (low + high) / 2
41+
42+
switch {
43+
case nums[median] < target:
44+
low = median + 1
45+
case nums[median] > target:
46+
high = median - 1
47+
default:
48+
return median
49+
}
50+
}
51+
return -1
52+
}
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package Problem0034
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_Problem0034(t *testing.T) {
@@ -30,17 +31,27 @@ func Test_Problem0034(t *testing.T) {
3031
qs := []question{
3132

3233
question{
33-
para{""},
34-
ans{""},
34+
para{[]int{5, 7, 7, 8, 8, 10}, 8},
35+
ans{[]int{3, 4}},
36+
},
37+
38+
question{
39+
para{[]int{5, 7, 7, 8, 8, 10}, 6},
40+
ans{[]int{-1, -1}},
3541
},
36-
42+
43+
question{
44+
para{[]int{5, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 10}, 8},
45+
ans{[]int{3, 12}},
46+
},
47+
3748
// 如需多个测试,可以复制上方元素。
3849
}
3950

4051
for _, q := range qs {
4152
a, p := q.ans, q.para
4253
fmt.Printf("~~%v~~\n", p)
4354

44-
ast.Equal(a.one, (p.one), "输入:%v", p)
55+
ast.Equal(a.one, searchRange(p.one, p.two), "输入:%v", p)
4556
}
4657
}

0 commit comments

Comments
 (0)