Skip to content

Commit c1a8312

Browse files
authored
Merge pull request #2 from kimi0230/master
Merge Leetcode
2 parents 37555fe + e6b7e3f commit c1a8312

File tree

73 files changed

+5754
-76
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+5754
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package findtargetlastindex
2+
3+
// RightBound, 有點類似 nums 大於 target的元素有幾個
4+
5+
// 二分搜尋
6+
func Solution(nums []int, target int) int {
7+
8+
left, right := 0, len(nums)-1
9+
10+
for left <= right {
11+
mid := int(uint(left+right) >> 1)
12+
if nums[mid] == target {
13+
if nums[right] == target {
14+
return right
15+
} else {
16+
right--
17+
}
18+
} else if nums[mid] < target {
19+
left = mid + 1
20+
} else {
21+
right = mid - 1
22+
}
23+
}
24+
return -1
25+
}
26+
27+
func SolutionRecursive(nums []int, target int) int {
28+
left, right := 0, len(nums)-1
29+
return findTarget(nums, left, right, target)
30+
}
31+
32+
func findTarget(nums []int, left, right, target int) int {
33+
if left > right {
34+
return -1
35+
}
36+
37+
mid := int(uint(left+right) >> 1)
38+
if nums[mid] == target {
39+
if nums[right] == target {
40+
return right
41+
} else {
42+
return findTarget(nums, mid, right-1, target)
43+
}
44+
} else if nums[mid] < target {
45+
return findTarget(nums, mid+1, right, target)
46+
} else {
47+
return findTarget(nums, left, mid-1, target)
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
---
2+
title: Find Target Last Index
3+
subtitle: ""
4+
date: 2023-11-16T17:09:00+08:00
5+
lastmod: 2023-11-16T17:09:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "在有序的array中 找出target在array中最後的index是什麼"
10+
license: ""
11+
images: []
12+
13+
tags: [Interview, Algorithms, Go, Easy, Find Target Last Index, Right Bound, Left Bound]
14+
categories: [Algorithms]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
62+
# Find Target Last Index
63+
64+
在有序的array中 找出target在array中最後的index是什麼
65+
用二分搜尋法去找, RightBound
66+
67+
可參考
68+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0704.Binary-Search/main.go
69+
---
70+
71+
## 方法一
72+
73+
```go
74+
func Solution(nums []int, target int) int {
75+
left, right := 0, len(nums)-1
76+
result := -1
77+
for left <= right {
78+
mid := int(uint(right+left) >> 1)
79+
if nums[mid] == target {
80+
// 繼續找, 縮小右邊
81+
if target == nums[right] {
82+
result = right
83+
break
84+
} else {
85+
right--
86+
}
87+
} else if nums[mid] < target {
88+
// 往右找
89+
left = mid + 1
90+
} else if nums[mid] > target {
91+
// 往左找
92+
right = mid - 1
93+
}
94+
}
95+
return result
96+
}
97+
```
98+
99+
---
100+
101+
## 方法二 遞迴
102+
103+
```go
104+
func SolutionRecursive(nums []int, target int) int {
105+
left, right := 0, len(nums)-1
106+
return findTarget(nums, left, right, target)
107+
}
108+
109+
func findTarget(nums []int, left, right, target int) int {
110+
if left > right {
111+
return -1
112+
}
113+
114+
mid := int(uint(left+right) >> 1)
115+
if nums[mid] == target {
116+
if nums[right] == target {
117+
return right
118+
} else {
119+
return findTarget(nums, mid, right-1, target)
120+
}
121+
} else if nums[mid] < target {
122+
return findTarget(nums, mid+1, right, target)
123+
} else {
124+
return findTarget(nums, left, mid-1, target)
125+
}
126+
127+
}
128+
129+
```
130+
131+
``` sh
132+
go test -benchmem -run=none LeetcodeGolang/Algorithms/Find_Target_Last_Index -bench=.
133+
goos: darwin
134+
goarch: amd64
135+
pkg: LeetcodeGolang/Algorithms/Find_Target_Last_Index
136+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
137+
BenchmarkSolution-8 54216429 24.75 ns/op 0 B/op 0 allocs/op
138+
BenchmarkSolutionRecursive-8 40756744 27.75 ns/op 0 B/op 0 allocs/op
139+
PASS
140+
ok LeetcodeGolang/Algorithms/Find_Target_Last_Index 2.537s
141+
```
142+
143+
---
144+
145+
## RightBound
146+
147+
```go
148+
// 有點類似 nums 大於 target的元素有幾個
149+
func RightBound(nums []int, target int) (index int) {
150+
lenght := len(nums)
151+
if lenght <= 0 {
152+
return -1
153+
}
154+
left, right := 0, lenght-1
155+
156+
for left <= right {
157+
// 除以2
158+
// mid := left + (right-left)>>1
159+
mid := int(uint(right+left) >> 1)
160+
if nums[mid] == target {
161+
// 注意:要繼續找右邊, 所以把左邊變大=mid+1
162+
left = mid + 1
163+
} else if nums[mid] < target {
164+
// 找右邊
165+
left = mid + 1
166+
} else if nums[mid] > target {
167+
// 找左邊
168+
right = mid - 1
169+
}
170+
}
171+
// 都沒找到 注意:right越界情況
172+
if right < 0 || nums[right] != target {
173+
return -1
174+
}
175+
return right
176+
}
177+
```
178+
179+
180+
## LeftBound
181+
182+
``` go
183+
// 有點類似 nums 小於 target的元素有幾個
184+
func LeftBound(nums []int, target int) (index int) {
185+
lenght := len(nums)
186+
if lenght <= 0 {
187+
return -1
188+
}
189+
left, right := 0, lenght-1
190+
191+
for left <= right {
192+
// 除以2
193+
// mid := left + (right-left)>>1
194+
mid := int(uint(right+left) >> 1)
195+
if nums[mid] == target {
196+
// 要繼續找左邊, 所以把右邊變小
197+
right = mid - 1
198+
} else if nums[mid] < target {
199+
// 找右邊
200+
left = mid + 1
201+
} else if nums[mid] > target {
202+
// 找左邊
203+
right = mid - 1
204+
}
205+
}
206+
// 都沒找到 注意: left越界情況
207+
if left >= lenght || nums[left] != target {
208+
return -1
209+
}
210+
return left
211+
}
212+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package findtargetlastindex
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
arg1 []int
7+
arg2 int
8+
want int
9+
}{
10+
{
11+
[]int{1, 2, 3, 4, 5, 6, 6, 6, 9},
12+
6,
13+
7,
14+
},
15+
{
16+
[]int{1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 8, 9, 9, 9, 9},
17+
6,
18+
12,
19+
},
20+
{
21+
[]int{1, 2, 3, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 8, 9, 9, 9, 9},
22+
5,
23+
6,
24+
},
25+
{
26+
[]int{5, 7, 7, 8, 8, 10},
27+
8,
28+
4,
29+
},
30+
{
31+
[]int{5, 7, 7, 8, 8, 10},
32+
6,
33+
-1,
34+
},
35+
{
36+
[]int{1, 2, 3, 4, 5},
37+
3,
38+
2,
39+
},
40+
{
41+
[]int{1, 2, 3, 4, 5},
42+
5,
43+
4,
44+
},
45+
{[]int{1, 2, 3, 4, 5},
46+
0,
47+
-1,
48+
},
49+
}
50+
51+
func TestSolution(t *testing.T) {
52+
for _, tt := range tests {
53+
if got := Solution(tt.arg1, tt.arg2); got != tt.want {
54+
t.Errorf("got = %v, want = %v", got, tt.want)
55+
}
56+
}
57+
}
58+
59+
func TestSolutionRecursive(t *testing.T) {
60+
for _, tt := range tests {
61+
// start := 0
62+
// end := len(tt.arg1) - 1
63+
if got := SolutionRecursive(tt.arg1, tt.arg2); got != tt.want {
64+
t.Errorf("got = %v, want = %v", got, tt.want)
65+
}
66+
67+
// SolutionRecursive(tt.arg1, start, end, tt.arg2)
68+
}
69+
}
70+
71+
func BenchmarkSolution(b *testing.B) {
72+
b.ResetTimer()
73+
for i := 0; i < b.N; i++ {
74+
for _, tt := range tests {
75+
Solution(tt.arg1, tt.arg2)
76+
}
77+
}
78+
}
79+
func BenchmarkSolutionRecursive(b *testing.B) {
80+
b.ResetTimer()
81+
for i := 0; i < b.N; i++ {
82+
for _, tt := range tests {
83+
SolutionRecursive(tt.arg1, tt.arg2)
84+
}
85+
}
86+
}
87+
88+
/*
89+
go test -benchmem -run=none LeetcodeGolang/Algorithms/Find_Target_Last_Index -bench=.
90+
goos: darwin
91+
goarch: amd64
92+
pkg: LeetcodeGolang/Algorithms/Find_Target_Last_Index
93+
cpu: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
94+
BenchmarkSolution-8 54216429 24.75 ns/op 0 B/op 0 allocs/op
95+
BenchmarkSolutionRecursive-8 40756744 27.75 ns/op 0 B/op 0 allocs/op
96+
PASS
97+
ok LeetcodeGolang/Algorithms/Find_Target_Last_Index 2.537s
98+
*/

0 commit comments

Comments
 (0)