Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb65a8d

Browse files
committed
add binary search template
1 parentecf250d commitbb65a8d

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

‎lib/Search.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Solution
2+
3+
//普通搜索
4+
funcBinarySearch(nums []int,targetint)int {
5+
left,right:=0,len(nums)-1
6+
7+
forleft<right {
8+
mid:=left+ (right-left)/2
9+
ifnums[mid]==target {
10+
returnmid
11+
}elseifnums[mid]<target {
12+
left=mid+1
13+
}elseifnums[mid]>target {
14+
right=mid-1
15+
}
16+
}
17+
18+
return-1
19+
}
20+
21+
//左边界
22+
funcBinarySearchLeftBound(nums []int,targetint)int {
23+
left,right:=0,len(nums)-1
24+
25+
forleft<=right {
26+
mid:=left+ (right-left)/2
27+
ifnums[mid]==target {
28+
right=mid-1
29+
}elseifnums[mid]<target {
30+
left=mid+1
31+
32+
}elseifnums[mid]>target {
33+
right=mid-1
34+
}
35+
}
36+
ifnums[left]!=target||left>len(nums) {
37+
return-1
38+
}
39+
returnleft
40+
}
41+
42+
//右边界
43+
funcBinarySearchRightBound(nums []int,targetint)int {
44+
left,right:=0,len(nums)-1
45+
46+
forleft<=right {
47+
mid:=left+ (right-left)/2
48+
ifnums[mid]==target {
49+
left=mid+1
50+
}elseifnums[mid]<target {
51+
left=mid+1
52+
53+
}elseifnums[mid]>target {
54+
right=mid-1
55+
}
56+
}
57+
ifright<0||nums[right]!=target {
58+
return-1
59+
}
60+
returnright
61+
}

‎lib/Search_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package Solution
2+
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
8+
9+
funcTestBinarySearch(t*testing.T) {
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputs []int
14+
targetint
15+
expectint
16+
}{
17+
{"TestCase", []int{1,2,2,2,3},2,2},
18+
}
19+
20+
//开始测试
21+
fori,c:=rangecases {
22+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
23+
got:=BinarySearch(c.inputs,c.target)
24+
if!reflect.DeepEqual(got,c.expect) {
25+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26+
c.expect,got,c.inputs)
27+
}
28+
})
29+
}
30+
}
31+
32+
funcTestBinarySearchLeftBound(t*testing.T) {
33+
//测试用例
34+
cases:= []struct {
35+
namestring
36+
inputs []int
37+
targetint
38+
expectint
39+
}{
40+
{"TestCase", []int{1,2,2,2,3},2,1},
41+
}
42+
43+
//开始测试
44+
fori,c:=rangecases {
45+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
46+
got:=BinarySearchLeftBound(c.inputs,c.target)
47+
if!reflect.DeepEqual(got,c.expect) {
48+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
49+
c.expect,got,c.inputs)
50+
}
51+
})
52+
}
53+
}
54+
55+
funcTestBinarySearchRightBound(t*testing.T) {
56+
//测试用例
57+
cases:= []struct {
58+
namestring
59+
inputs []int
60+
targetint
61+
expectint
62+
}{
63+
{"TestCase", []int{1,2,2,2,3},2,3},
64+
}
65+
66+
//开始测试
67+
fori,c:=rangecases {
68+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
69+
got:=BinarySearchRightBound(c.inputs,c.target)
70+
if!reflect.DeepEqual(got,c.expect) {
71+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
72+
c.expect,got,c.inputs)
73+
}
74+
})
75+
}
76+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp