diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md index c179b0fbec487..ac9087718a338 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md @@ -69,17 +69,19 @@ tags: ### 方法一:二分查找 -我们定义二分查找的左边界 $l=0$,右边界 $r=n-1$,其中 $n$ 为数组的长度。 +我们定义二分查找的左边界 $l = 0$,右边界 $r = n - 1$,其中 $n$ 为数组的长度。 + +每次在二分查找的过程中,我们会得到当前的中点 $\textit{mid} = (l + r) / 2$。 -每次在二分查找的过程中,我们会得到当前的中点 $mid=(l+r)/2$。 +- 如果 $\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]$。 +- 如果 $\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}]$。 +- 如果 $\textit{nums}[\textit{mid}] = \textit{nums}[r]$,说明元素 $\textit{nums}[\textit{mid}]$ 和 $\textit{nums}[r]$ 相等,此时无法判断 $\textit{target}$ 位于哪个区间,我们只能将 $r$ 减少 $1$。 -- 如果 $nums[mid] \gt nums[r]$,说明 $[l,mid]$ 是有序的,此时如果 $nums[l] \le target \le nums[mid]$,说明 $target$ 位于 $[l,mid]$,否则 $target$ 位于 $[mid+1,r]$。 -- 如果 $nums[mid] \lt nums[r]$,说明 $[mid+1,r]$ 是有序的,此时如果 $nums[mid] \lt target \le nums[r]$,说明 $target$ 位于 $[mid+1,r]$,否则 $target$ 位于 $[l,mid]$。 -- 如果 $nums[mid] = nums[r]$,说明元素 $nums[mid]$ 和 $nums[r]$ 相等,此时无法判断 $target$ 位于哪个区间,我们只能将 $r$ 减少 $1$。 +二分查找结束后,如果 $\textit{nums}[l] = \textit{target}$,则说明数组中存在目标值 $\textit{target}$,否则说明不存在。 -二分查找结束后,如果 $nums[l] = target$,则说明数组中存在目标值 $target$,否则说明不存在。 +时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。 -时间复杂度近似 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 为数组的长度。 +我们定义二分查找的左边界 $l=0$,右边界 $r=n-1$,其中 $n$ 为数组的长度。 @@ -220,6 +222,67 @@ function search(nums: number[], target: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn search(nums: Vec, target: i32) -> bool { + let (mut l, mut r) = (0, nums.len() - 1); + while l < r { + let mid = (l + r) >> 1; + if nums[mid] > nums[r] { + if nums[l] <= target && target <= nums[mid] { + r = mid; + } else { + l = mid + 1; + } + } else if nums[mid] < nums[r] { + if nums[mid] < target && target <= nums[r] { + l = mid + 1; + } else { + r = mid; + } + } else { + r -= 1; + } + } + nums[l] == target + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} target + * @return {boolean} + */ +var search = function (nums, target) { + let [l, r] = [0, nums.length - 1]; + while (l < r) { + const mid = (l + r) >> 1; + if (nums[mid] > nums[r]) { + if (nums[l] <= target && target <= nums[mid]) { + r = mid; + } else { + l = mid + 1; + } + } else if (nums[mid] < nums[r]) { + if (nums[mid] < target && target <= nums[r]) { + l = mid + 1; + } else { + r = mid; + } + } else { + --r; + } + } + return nums[l] === target; +}; +``` + diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md index 2e7c9c971a319..3b464b9a648f1 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md @@ -54,17 +54,17 @@ tags: ### Solution 1: Binary Search -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. +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. -During each binary search process, we get the current midpoint $mid=(l+r)/2$. +Each time during the binary search, we get the current midpoint $\textit{mid} = (l + r) / 2$. -- 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]$. -- 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]$. -- 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$. +- 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]$. +- 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}]$. +- 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$. -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. +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. -The time complexity is approximately $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array. +The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$. @@ -205,6 +205,67 @@ function search(nums: number[], target: number): boolean { } ``` +#### Rust + +```rust +impl Solution { + pub fn search(nums: Vec, target: i32) -> bool { + let (mut l, mut r) = (0, nums.len() - 1); + while l < r { + let mid = (l + r) >> 1; + if nums[mid] > nums[r] { + if nums[l] <= target && target <= nums[mid] { + r = mid; + } else { + l = mid + 1; + } + } else if nums[mid] < nums[r] { + if nums[mid] < target && target <= nums[r] { + l = mid + 1; + } else { + r = mid; + } + } else { + r -= 1; + } + } + nums[l] == target + } +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @param {number} target + * @return {boolean} + */ +var search = function (nums, target) { + let [l, r] = [0, nums.length - 1]; + while (l < r) { + const mid = (l + r) >> 1; + if (nums[mid] > nums[r]) { + if (nums[l] <= target && target <= nums[mid]) { + r = mid; + } else { + l = mid + 1; + } + } else if (nums[mid] < nums[r]) { + if (nums[mid] < target && target <= nums[r]) { + l = mid + 1; + } else { + r = mid; + } + } else { + --r; + } + } + return nums[l] === target; +}; +``` + diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.js b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.js new file mode 100644 index 0000000000000..4a762eee420ab --- /dev/null +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.js @@ -0,0 +1,27 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {boolean} + */ +var search = function (nums, target) { + let [l, r] = [0, nums.length - 1]; + while (l < r) { + const mid = (l + r) >> 1; + if (nums[mid] > nums[r]) { + if (nums[l] <= target && target <= nums[mid]) { + r = mid; + } else { + l = mid + 1; + } + } else if (nums[mid] < nums[r]) { + if (nums[mid] < target && target <= nums[r]) { + l = mid + 1; + } else { + r = mid; + } + } else { + --r; + } + } + return nums[l] === target; +}; diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.rs b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.rs new file mode 100644 index 0000000000000..14abf0bd8bd0f --- /dev/null +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.rs @@ -0,0 +1,24 @@ +impl Solution { + pub fn search(nums: Vec, target: i32) -> bool { + let (mut l, mut r) = (0, nums.len() - 1); + while l < r { + let mid = (l + r) >> 1; + if nums[mid] > nums[r] { + if nums[l] <= target && target <= nums[mid] { + r = mid; + } else { + l = mid + 1; + } + } else if nums[mid] < nums[r] { + if nums[mid] < target && target <= nums[r] { + l = mid + 1; + } else { + r = mid; + } + } else { + r -= 1; + } + } + nums[l] == target + } +}