Skip to content

Commit 59b948f

Browse files
committed
feat: 34. Find First and Last Position of Element in Sorted Array
1 parent 2ea8db2 commit 59b948f

File tree

5 files changed

+280
-6
lines changed

5 files changed

+280
-6
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: 0034. Find First and Last Position of Element in Sorted Array
3+
subtitle: "https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/"
4+
date: 2024-01-22T21:20:00+08:00
5+
lastmod: 2024-01-22T21:20:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Find First and Last Position of Element in Sorted Array, Array, Binary Search]
14+
categories: [LeetCode]
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+
# [0034. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
70+
## Big O
71+
72+
* 時間複雜 : `O(log(n))`
73+
* 空間複雜 : `O(1)`
74+
75+
## 來源
76+
* https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/
77+
* https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/
78+
79+
## 解答
80+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/main.go
81+
82+
```go
83+
package findfirstandlastpositionofelementinsortedarray
84+
85+
// 時間複雜 O(log(n)), 空間複雜 O(1)
86+
func searchRange(nums []int, target int) []int {
87+
result := []int{}
88+
89+
// first index
90+
left, right := 0, len(nums)-1
91+
for left < right {
92+
mid := int(uint(left+right) >> 1) // [0,1] => 0
93+
if nums[mid] < target {
94+
left = mid + 1
95+
} else if nums[mid] > target {
96+
right = mid - 1
97+
} else {
98+
right = mid
99+
}
100+
}
101+
if left == right && nums[left] == target {
102+
result = append(result, left)
103+
} else {
104+
result = append(result, -1)
105+
}
106+
107+
// last index
108+
left, right = 0, len(nums)-1
109+
for left < right {
110+
mid := int(uint(left+right+1) >> 1) // [0,1] => 1
111+
if nums[mid] < target {
112+
left = mid + 1
113+
} else if nums[mid] > target {
114+
right = mid - 1
115+
} else {
116+
left = mid
117+
}
118+
}
119+
if left == right && nums[left] == target {
120+
result = append(result, left)
121+
} else {
122+
result = append(result, -1)
123+
}
124+
return result
125+
}
126+
127+
```
128+
129+
## Benchmark
130+
131+
```sh
132+
133+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package findfirstandlastpositionofelementinsortedarray
2+
3+
// 時間複雜 O(log(n)), 空間複雜 O(1)
4+
func searchRange(nums []int, target int) []int {
5+
result := []int{}
6+
7+
// first index
8+
left, right := 0, len(nums)-1
9+
for left < right {
10+
mid := int(uint(left+right) >> 1) // [0,1] => 0
11+
if nums[mid] < target {
12+
left = mid + 1
13+
} else if nums[mid] > target {
14+
right = mid - 1
15+
} else {
16+
right = mid
17+
}
18+
}
19+
if left == right && nums[left] == target {
20+
result = append(result, left)
21+
} else {
22+
result = append(result, -1)
23+
}
24+
25+
// last index
26+
left, right = 0, len(nums)-1
27+
for left < right {
28+
mid := int(uint(left+right+1) >> 1) // [0,1] => 1
29+
if nums[mid] < target {
30+
left = mid + 1
31+
} else if nums[mid] > target {
32+
right = mid - 1
33+
} else {
34+
left = mid
35+
}
36+
}
37+
if left == right && nums[left] == target {
38+
result = append(result, left)
39+
} else {
40+
result = append(result, -1)
41+
}
42+
return result
43+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package findfirstandlastpositionofelementinsortedarray
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
nums []int
7+
target int
8+
want []int
9+
}{
10+
{[]int{5, 7, 7, 8, 8, 10}, 8, []int{3, 4}},
11+
{[]int{5, 7, 7, 8, 8, 10}, 6, []int{-1, -1}},
12+
{[]int{}, 0, []int{-1, -1}},
13+
{[]int{1}, 1, []int{0, 0}},
14+
}
15+
16+
func TestSearchRange(t *testing.T) {
17+
for _, tt := range tests {
18+
got := searchRange(tt.nums, tt.target)
19+
if got[0] != tt.want[0] || got[1] != tt.want[1] {
20+
t.Errorf("searchRange(%v, %v) = %v, want %v", tt.nums, tt.target, got, tt.want)
21+
}
22+
}
23+
}
24+
25+
func BenchmarkSearchRange(b *testing.B) {
26+
for i := 0; i < b.N; i++ {
27+
for _, tt := range tests {
28+
searchRange(tt.nums, tt.target)
29+
}
30+
}
31+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 時間複雜度:
2+
// 空間複雜度:
3+
/*
4+
* @lc app=leetcode.cn id=34 lang=golang
5+
*
6+
* [34] 在排序数组中查找元素的第一个和最后一个位置
7+
*/
8+
9+
// @lc code=start
10+
func searchRange(nums []int, target int) []int {
11+
result := []int{}
12+
13+
// first index
14+
left, right := 0, len(nums)-1
15+
for left < right {
16+
mid := int(uint(left+right) >> 1) // [0,1] => 0
17+
if nums[mid] < target {
18+
left = mid + 1
19+
} else if nums[mid] > target {
20+
right = mid - 1
21+
} else {
22+
right = mid
23+
}
24+
}
25+
if left == right && nums[left] == target {
26+
result = append(result, left)
27+
} else {
28+
result = append(result, -1)
29+
}
30+
31+
// last index
32+
left, right = 0, len(nums)-1
33+
for left < right {
34+
mid := int(uint(left+right+1) >> 1) // [0,1] => 1
35+
if nums[mid] < target {
36+
left = mid + 1
37+
} else if nums[mid] > target {
38+
right = mid - 1
39+
} else {
40+
left = mid
41+
}
42+
}
43+
if left == right && nums[left] == target {
44+
result = append(result, left)
45+
} else {
46+
result = append(result, -1)
47+
}
48+
return result
49+
}
50+
51+
// @lc code=end
52+

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ int binarySearch(int[] nums, int target){
397397
```
398398

399399
**將搜尋區間全部統一成兩端都閉**, 方便記憶
400+
`[letf,right]`
400401

401402
```go
402403
func Search(nums []int, target int) int {
@@ -482,12 +483,26 @@ func RightBound(nums []int, target int) (index int) {
482483
return right
483484
}
484485
```
485-
| No. | Title | Solution | Difficulty | Time | Space | Topic |
486-
|-------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------:|------------|---------------------------------------|-------------------------------|---------------|
487-
| [0704](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0704.Binary-Search/) | [704. Binary Search](https://leetcode.com/problems/binary-search/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0704.Binary-Search) | Easy | 最差:O(long n)<br> 最佳O(1)剛好在中間 | 迭代: O(1) <br/> 遞迴O(log n) | Binary Search |
488-
| [0875](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0875.Koko-Eating-Bananas/) | [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0875.Koko-Eating-Bananas) | Medium | O(n log m) | O(1) | Binary Search |
489-
| [0153](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0153.Find-Minimum-in-Rotated-Sorted-Array/) | [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0153.Find-Minimum-in-Rotated-Sorted-Array) | Medium | O(log n) | O(1) | Binary Search |
490-
| [0033](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0033.Search-in-Rotated-Sorted-Array/) | [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description//) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0033.Search-in-Rotated-Sorted-Array) | Medium | O(log n) | O(1) | Binary Search |
486+
487+
`for left <= right {}`:
488+
489+
* 此方法在迭代終止時,left 和 right 指針指向同一個位置。
490+
* 此方法會包括最後一個可能的候選解,在某些情況下可能更容易理解和實現。
491+
* 在應對邊界情況時可能更為方便,例如當 left 和 right 指向同一位置時,這時可能需要進一步處理。
492+
493+
`for left < right {}`:
494+
495+
* 此方法在迭代終止時,left 和 right 指針指向相鄰位置。
496+
* 此方法會排除最後一個可能的候選解,當要求嚴格小於或大於某個值時可能更合適。
497+
* 在一些情況下,可能會更高效,因為在每次循環中只需要比較一次 left 和 right,而不需要再處理當兩者相等時的情況。
498+
499+
| No. | Title | Solution | Difficulty | Time | Space | Topic |
500+
|--------------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------:|------------|---------------------------------------|-------------------------------|---------------|
501+
| [0704](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0704.Binary-Search/) | [704. Binary Search](https://leetcode.com/problems/binary-search/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0704.Binary-Search) | Easy | 最差:O(long n)<br> 最佳O(1)剛好在中間 | 迭代: O(1) <br/> 遞迴O(log n) | Binary Search |
502+
| [0875](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0875.Koko-Eating-Bananas/) | [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0875.Koko-Eating-Bananas) | Medium | O(n log m) | O(1) | Binary Search |
503+
| [0153](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0153.Find-Minimum-in-Rotated-Sorted-Array/) | [153. Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0153.Find-Minimum-in-Rotated-Sorted-Array) | Medium | O(log n) | O(1) | Binary Search |
504+
| [0033](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0033.Search-in-Rotated-Sorted-Array/) | [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description//) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0033.Search-in-Rotated-Sorted-Array) | Medium | O(log n) | O(1) | Binary Search |
505+
| [0034](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array/) | [34. Find First and Last Position of Element in Sorted Array](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/description/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0034.Find-First-and-Last-Position-of-Element-in-Sorted-Array) | Medium | O(log n) | O(1) | Binary Search |
491506
492507
493508
---

0 commit comments

Comments
 (0)