Skip to content

Commit 4faa639

Browse files
authored
feat: add solutions to lc problem: No.0081 (#4010)
No.0081.Search in Rotated Sorted Array II
1 parent f1e3da2 commit 4faa639

File tree

4 files changed

+189
-14
lines changed

4 files changed

+189
-14
lines changed

solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md

+70-7
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ tags:
6969

7070
### 方法一:二分查找
7171

72-
我们定义二分查找的左边界 $l=0$,右边界 $r=n-1$,其中 $n$ 为数组的长度。
72+
我们定义二分查找的左边界 $l = 0$,右边界 $r = n - 1$,其中 $n$ 为数组的长度。
73+
74+
每次在二分查找的过程中,我们会得到当前的中点 $\textit{mid} = (l + r) / 2$。
7375

74-
每次在二分查找的过程中,我们会得到当前的中点 $mid=(l+r)/2$。
76+
- 如果 $\textit{nums}[\textit{mid}] > \textit{nums}[r]$,说明 $[l, \textit{mid}]$ 是有序的,此时如果 $\textit{nums}[l] \le \textit{target} \le \textit{nums}[\textit{mid}]$,说明 $\textit{target}$ 位于 $[l, \textit{mid}]$,否则 $\textit{target}$ 位于 $[\textit{mid} + 1, r]$。
77+
- 如果 $\textit{nums}[\textit{mid}] < \textit{nums}[r]$,说明 $[\textit{mid} + 1, r]$ 是有序的,此时如果 $\textit{nums}[\textit{mid}] < \textit{target} \le \textit{nums}[r]$,说明 $\textit{target}$ 位于 $[\textit{mid} + 1, r]$,否则 $\textit{target}$ 位于 $[l, \textit{mid}]$。
78+
- 如果 $\textit{nums}[\textit{mid}] = \textit{nums}[r]$,说明元素 $\textit{nums}[\textit{mid}]$ 和 $\textit{nums}[r]$ 相等,此时无法判断 $\textit{target}$ 位于哪个区间,我们只能将 $r$ 减少 $1$。
7579

76-
- 如果 $nums[mid] \gt nums[r]$,说明 $[l,mid]$ 是有序的,此时如果 $nums[l] \le target \le nums[mid]$,说明 $target$ 位于 $[l,mid]$,否则 $target$ 位于 $[mid+1,r]$。
77-
- 如果 $nums[mid] \lt nums[r]$,说明 $[mid+1,r]$ 是有序的,此时如果 $nums[mid] \lt target \le nums[r]$,说明 $target$ 位于 $[mid+1,r]$,否则 $target$ 位于 $[l,mid]$。
78-
- 如果 $nums[mid] = nums[r]$,说明元素 $nums[mid]$ 和 $nums[r]$ 相等,此时无法判断 $target$ 位于哪个区间,我们只能将 $r$ 减少 $1$。
80+
二分查找结束后,如果 $\textit{nums}[l] = \textit{target}$,则说明数组中存在目标值 $\textit{target}$,否则说明不存在。
7981

80-
二分查找结束后,如果 $nums[l] = target$,则说明数组中存在目标值 $target$,否则说明不存在
82+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$
8183

82-
时间复杂度近似 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。
84+
我们定义二分查找的左边界 $l=0$,右边界 $r=n-1$,其中 $n$ 为数组的长度。
8385

8486
<!-- tabs:start -->
8587

@@ -220,6 +222,67 @@ function search(nums: number[], target: number): boolean {
220222
}
221223
```
222224

225+
#### Rust
226+
227+
```rust
228+
impl Solution {
229+
pub fn search(nums: Vec<i32>, target: i32) -> bool {
230+
let (mut l, mut r) = (0, nums.len() - 1);
231+
while l < r {
232+
let mid = (l + r) >> 1;
233+
if nums[mid] > nums[r] {
234+
if nums[l] <= target && target <= nums[mid] {
235+
r = mid;
236+
} else {
237+
l = mid + 1;
238+
}
239+
} else if nums[mid] < nums[r] {
240+
if nums[mid] < target && target <= nums[r] {
241+
l = mid + 1;
242+
} else {
243+
r = mid;
244+
}
245+
} else {
246+
r -= 1;
247+
}
248+
}
249+
nums[l] == target
250+
}
251+
}
252+
```
253+
254+
#### JavaScript
255+
256+
```js
257+
/**
258+
* @param {number[]} nums
259+
* @param {number} target
260+
* @return {boolean}
261+
*/
262+
var search = function (nums, target) {
263+
let [l, r] = [0, nums.length - 1];
264+
while (l < r) {
265+
const mid = (l + r) >> 1;
266+
if (nums[mid] > nums[r]) {
267+
if (nums[l] <= target && target <= nums[mid]) {
268+
r = mid;
269+
} else {
270+
l = mid + 1;
271+
}
272+
} else if (nums[mid] < nums[r]) {
273+
if (nums[mid] < target && target <= nums[r]) {
274+
l = mid + 1;
275+
} else {
276+
r = mid;
277+
}
278+
} else {
279+
--r;
280+
}
281+
}
282+
return nums[l] === target;
283+
};
284+
```
285+
223286
<!-- tabs:end -->
224287

225288
<!-- solution:end -->

solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md

+68-7
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ tags:
5454

5555
### Solution 1: Binary Search
5656

57-
We define the left boundary $l=0$ and the right boundary $r=n-1$ for the binary search, where $n$ is the length of the array.
57+
We define the left boundary of the binary search as $l = 0$ and the right boundary as $r = n - 1$, where $n$ is the length of the array.
5858

59-
During each binary search process, we get the current midpoint $mid=(l+r)/2$.
59+
Each time during the binary search, we get the current midpoint $\textit{mid} = (l + r) / 2$.
6060

61-
- If $nums[mid] \gt nums[r]$, it means that $[l,mid]$ is ordered. At this time, if $nums[l] \le target \le nums[mid]$, it means that $target$ is in $[l,mid]$, otherwise $target$ is in $[mid+1,r]$.
62-
- If $nums[mid] \lt nums[r]$, it means that $[mid+1,r]$ is ordered. At this time, if $nums[mid] \lt target \le nums[r]$, it means that $target$ is in $[mid+1,r]$, otherwise $target$ is in $[l,mid]$.
63-
- If $nums[mid] = nums[r]$, it means that the elements $nums[mid]$ and $nums[r]$ are equal. At this time, we cannot determine which interval $target$ is in, so we can only decrease $r$ by $1$.
61+
- If $\textit{nums}[\textit{mid}] > \textit{nums}[r]$, it means $[l, \textit{mid}]$ is ordered. If $\textit{nums}[l] \le \textit{target} \le \textit{nums}[\textit{mid}]$, it means $\textit{target}$ is in $[l, \textit{mid}]$. Otherwise, $\textit{target}$ is in $[\textit{mid} + 1, r]$.
62+
- If $\textit{nums}[\textit{mid}] < \textit{nums}[r]$, it means $[\textit{mid} + 1, r]$ is ordered. If $\textit{nums}[\textit{mid}] < \textit{target} \le \textit{nums}[r]$, it means $\textit{target}$ is in $[\textit{mid} + 1, r]$. Otherwise, $\textit{target}$ is in $[l, \textit{mid}]$.
63+
- If $\textit{nums}[\textit{mid}] = \textit{nums}[r]$, it means the elements $\textit{nums}[\textit{mid}]$ and $\textit{nums}[r]$ are equal. In this case, we cannot determine which interval $\textit{target}$ is in, so we can only decrease $r$ by $1$.
6464

65-
After the binary search ends, if $nums[l] = target$, it means that the target value $target$ exists in the array, otherwise it means it does not exist.
65+
After the binary search, if $\textit{nums}[l] = \textit{target}$, it means the target value $\textit{target}$ exists in the array. Otherwise, it does not exist.
6666

67-
The time complexity is approximately $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array.
67+
The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.
6868

6969
<!-- tabs:start -->
7070

@@ -205,6 +205,67 @@ function search(nums: number[], target: number): boolean {
205205
}
206206
```
207207

208+
#### Rust
209+
210+
```rust
211+
impl Solution {
212+
pub fn search(nums: Vec<i32>, target: i32) -> bool {
213+
let (mut l, mut r) = (0, nums.len() - 1);
214+
while l < r {
215+
let mid = (l + r) >> 1;
216+
if nums[mid] > nums[r] {
217+
if nums[l] <= target && target <= nums[mid] {
218+
r = mid;
219+
} else {
220+
l = mid + 1;
221+
}
222+
} else if nums[mid] < nums[r] {
223+
if nums[mid] < target && target <= nums[r] {
224+
l = mid + 1;
225+
} else {
226+
r = mid;
227+
}
228+
} else {
229+
r -= 1;
230+
}
231+
}
232+
nums[l] == target
233+
}
234+
}
235+
```
236+
237+
#### JavaScript
238+
239+
```js
240+
/**
241+
* @param {number[]} nums
242+
* @param {number} target
243+
* @return {boolean}
244+
*/
245+
var search = function (nums, target) {
246+
let [l, r] = [0, nums.length - 1];
247+
while (l < r) {
248+
const mid = (l + r) >> 1;
249+
if (nums[mid] > nums[r]) {
250+
if (nums[l] <= target && target <= nums[mid]) {
251+
r = mid;
252+
} else {
253+
l = mid + 1;
254+
}
255+
} else if (nums[mid] < nums[r]) {
256+
if (nums[mid] < target && target <= nums[r]) {
257+
l = mid + 1;
258+
} else {
259+
r = mid;
260+
}
261+
} else {
262+
--r;
263+
}
264+
}
265+
return nums[l] === target;
266+
};
267+
```
268+
208269
<!-- tabs:end -->
209270

210271
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {boolean}
5+
*/
6+
var search = function (nums, target) {
7+
let [l, r] = [0, nums.length - 1];
8+
while (l < r) {
9+
const mid = (l + r) >> 1;
10+
if (nums[mid] > nums[r]) {
11+
if (nums[l] <= target && target <= nums[mid]) {
12+
r = mid;
13+
} else {
14+
l = mid + 1;
15+
}
16+
} else if (nums[mid] < nums[r]) {
17+
if (nums[mid] < target && target <= nums[r]) {
18+
l = mid + 1;
19+
} else {
20+
r = mid;
21+
}
22+
} else {
23+
--r;
24+
}
25+
}
26+
return nums[l] === target;
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
impl Solution {
2+
pub fn search(nums: Vec<i32>, target: i32) -> bool {
3+
let (mut l, mut r) = (0, nums.len() - 1);
4+
while l < r {
5+
let mid = (l + r) >> 1;
6+
if nums[mid] > nums[r] {
7+
if nums[l] <= target && target <= nums[mid] {
8+
r = mid;
9+
} else {
10+
l = mid + 1;
11+
}
12+
} else if nums[mid] < nums[r] {
13+
if nums[mid] < target && target <= nums[r] {
14+
l = mid + 1;
15+
} else {
16+
r = mid;
17+
}
18+
} else {
19+
r -= 1;
20+
}
21+
}
22+
nums[l] == target
23+
}
24+
}

0 commit comments

Comments
 (0)