Skip to content

Commit 24fa018

Browse files
committedMar 31, 2024·
feat: 33. Search in Rotated Sorted Array
1 parent 70a63c2 commit 24fa018

File tree

6 files changed

+304
-0
lines changed

6 files changed

+304
-0
lines changed
 
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
---
2+
title: 33. Search in Rotated Sorted Array
3+
subtitle: "https://leetcode.com/problems/search-in-rotated-sorted-array/description/"
4+
date: 2024-03-31T16:26:00+08:00
5+
lastmod: 2024-03-31T16:26:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0033.Search-in-Rotated-Sorted-Array"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Array, Binary Search, 33. Search in Rotated Sorted Array, Amazon,Facebook, Microsoft, LinkedIn, Apple]
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+
# [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/description/)
62+
63+
## 題目
64+
There is an integer array nums sorted in ascending order (with distinct values).
65+
66+
Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
67+
68+
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
69+
70+
You must write an algorithm with O(log n) runtime complexity.
71+
72+
73+
74+
Example 1:
75+
76+
Input: nums = [4,5,6,7,0,1,2], target = 0
77+
Output: 4
78+
Example 2:
79+
80+
Input: nums = [4,5,6,7,0,1,2], target = 3
81+
Output: -1
82+
Example 3:
83+
84+
Input: nums = [1], target = 0
85+
Output: -1
86+
87+
88+
Constraints:
89+
90+
1 <= nums.length <= 5000
91+
-104 <= nums[i] <= 104
92+
All values of nums are unique.
93+
nums is an ascending array that is possibly rotated.
94+
-104 <= target <= 104
95+
96+
## 題目大意
97+
整個數組按照升序排序, 值不相同
98+
在傳遞給函數之前, nums 在預先未知的某個下標 k ( 0 <= k < nums.length )上進行了 旋轉,使數位變數 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (下標 從 0 開始 計數)。 例如, [0,1,2,4,5,6,7] 在下標 3 處經旋轉後可能變為 [4,5,6,7,0,1,2]
99+
100+
給你 旋轉後 的陣列 nums 和一個整數 target ,如果 nums 中存在這個目標值 target ,則返回它的下標,否則返回 -1 。
101+
102+
你必須設計一個時間複雜度為 O(log n) 的演演算法解決此問題。
103+
104+
## 解題思路
105+
106+
先判斷mid位於上半部還是下半部, 再來找target位於哪邊
107+
最後看target是否有在左或右端點
108+
109+
![](0033.Search-in-Rotated-Sorted-Array.jpg)
110+
111+
112+
## Big O
113+
114+
* 時間複雜 : `O(log n)`
115+
* 空間複雜 : `O(1)`
116+
117+
## 來源
118+
* https://leetcode.com/problems/search-in-rotated-sorted-array/description/
119+
* https://leetcode.cn/problems/
120+
121+
## 解答
122+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0033.Search-in-Rotated-Sorted-Array/main.go
123+
124+
```go
125+
package searchinrotatedsortedarray
126+
127+
// 時間複雜 O(), 空間複雜 O()
128+
func search(nums []int, target int) int {
129+
if nums == nil || len(nums) == 0 {
130+
return -1
131+
}
132+
left, right := 0, len(nums)-1
133+
for left < right {
134+
mid := int(uint(left+right) >> 1)
135+
136+
if nums[mid] == target {
137+
return mid
138+
}
139+
140+
if nums[mid] >= nums[left] {
141+
// mid在上半區
142+
143+
if nums[left] <= target && target <= nums[mid] {
144+
// target 在left跟min中間
145+
right = mid - 1
146+
} else {
147+
left = mid + 1
148+
}
149+
} else {
150+
// mid在下半區
151+
if nums[mid] <= target && target <= nums[right] {
152+
// target 在min跟right中間
153+
left = mid + 1
154+
} else {
155+
right = mid - 1
156+
}
157+
}
158+
}
159+
if nums[left] == target {
160+
return left
161+
} else if nums[right] == target {
162+
return right
163+
} else {
164+
return -1
165+
}
166+
}
167+
168+
```
169+
170+
## Benchmark
171+
172+
```sh
173+
174+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package searchinrotatedsortedarray
2+
3+
// 時間複雜 O(), 空間複雜 O()
4+
func search(nums []int, target int) int {
5+
if nums == nil || len(nums) == 0 {
6+
return -1
7+
}
8+
left, right := 0, len(nums)-1
9+
for left < right {
10+
mid := int(uint(left+right) >> 1)
11+
12+
if nums[mid] == target {
13+
return mid
14+
}
15+
16+
if nums[mid] >= nums[left] {
17+
// mid在上半區
18+
19+
if nums[left] <= target && target <= nums[mid] {
20+
// target 在left跟min中間
21+
right = mid - 1
22+
} else {
23+
left = mid + 1
24+
}
25+
} else {
26+
// mid在下半區
27+
if nums[mid] <= target && target <= nums[right] {
28+
// target 在min跟right中間
29+
left = mid + 1
30+
} else {
31+
right = mid - 1
32+
}
33+
}
34+
}
35+
if nums[left] == target {
36+
return left
37+
} else if nums[right] == target {
38+
return right
39+
} else {
40+
return -1
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package searchinrotatedsortedarray
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var tests = []struct {
8+
nums []int
9+
target int
10+
want int
11+
}{
12+
{[]int{4, 5, 6, 7, 0, 1, 2}, 0, 4},
13+
{[]int{4, 5, 6, 7, 0, 1, 2}, 3, -1},
14+
{[]int{1}, 0, -1},
15+
}
16+
17+
func TestSearch(t *testing.T) {
18+
for _, tt := range tests {
19+
if got := search(tt.nums, tt.target); got != tt.want {
20+
t.Errorf("search(%v, %v) = %v, want %v", tt.nums, tt.target, got, tt.want)
21+
}
22+
}
23+
}
24+
25+
func BenchmarkSearch(b *testing.B) {
26+
for i := 0; i < b.N; i++ {
27+
for _, tt := range tests {
28+
search(tt.nums, tt.target)
29+
}
30+
}
31+
}
32+
33+
/*
34+
go test -benchmem -run=none LeetcodeGolang/Leetcode/0354.Russian-Doll-Envelopes -bench=.
35+
36+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// 時間複雜度:
2+
// 空間複雜度:
3+
/*
4+
* @lc app=leetcode.cn id=33 lang=golang
5+
*
6+
* [33] 搜索旋转排序数组
7+
*/
8+
9+
// @lc code=start
10+
func search(nums []int, target int) int {
11+
if nums == nil || len(nums) == 0 {
12+
return -1
13+
}
14+
left, right := 0, len(nums)-1
15+
for left < right {
16+
mid := int(uint(left+right) >> 1)
17+
18+
if nums[mid] == target {
19+
return mid
20+
}
21+
22+
if nums[mid] >= nums[left] {
23+
// mid在上半區
24+
25+
if nums[left] <= target && target <= nums[mid] {
26+
// target 在left跟min中間
27+
right = mid - 1
28+
} else {
29+
left = mid + 1
30+
}
31+
} else {
32+
// mid在下半區
33+
if nums[mid] <= target && target <= nums[right] {
34+
// target 在min跟right中間
35+
left = mid + 1
36+
} else {
37+
right = mid - 1
38+
}
39+
}
40+
}
41+
if nums[left] == target {
42+
return left
43+
} else if nums[right] == target {
44+
return right
45+
} else {
46+
return -1
47+
}
48+
}
49+
50+
// @lc code=end
51+

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ func RightBound(nums []int, target int) (index int) {
487487
| [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 |
488488
| [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 |
489489
| [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 |
490491

491492

492493
---

0 commit comments

Comments
 (0)
Please sign in to comment.