diff --git a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md index 151656d9d651c..361728caaea79 100644 --- a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md +++ b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md @@ -64,58 +64,7 @@ tags: 我们可以进行两次二分查找,分别查找出左边界和右边界。 -时间复杂度 $O(\log n)$,空间复杂度 $O(1)$。其中 $n$ 是数组 $nums$ 的长度。 - -以下是二分查找的两个通用模板: - -模板 1: - -```java -boolean check(int x) { -} - -int search(int left, int right) { - while (left < right) { - int mid = (left + right) >> 1; - if (check(mid)) { - right = mid; - } else { - left = mid + 1; - } - } - return left; -} -``` - -模板 2: - -```java -boolean check(int x) { -} - -int search(int left, int right) { - while (left < right) { - int mid = (left + right + 1) >> 1; - if (check(mid)) { - left = mid; - } else { - right = mid - 1; - } - } - return left; -} -``` - -做二分题目时,可以按照以下套路: - -1. 写出循环条件 $left < right$; -1. 循环体内,不妨先写 $mid = \lfloor \frac{left + right}{2} \rfloor$; -1. 根据具体题目,实现 $check()$ 函数(有时很简单的逻辑,可以不定义 $check$),想一下究竟要用 $right = mid$(模板 $1$) 还是 $left = mid$(模板 $2$); -     - 如果 $right = mid$,那么写出 else 语句 $left = mid + 1$,并且不需要更改 mid 的计算,即保持 $mid = \lfloor \frac{left + right}{2} \rfloor$; -     - 如果 $left = mid$,那么写出 else 语句 $right = mid - 1$,并且在 $mid$ 计算时补充 +1,即 $mid = \lfloor \frac{left + right + 1}{2} \rfloor$; -1. 循环结束时, $left$ 与 $right$ 相等。 - -注意,这两个模板的优点是始终保持答案位于二分区间内,二分结束条件对应的值恰好在答案所处的位置。 对于可能无解的情况,只要判断二分结束后的 $left$ 或者 $right$ 是否满足题意即可。 +时间复杂度 $O(\log n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -162,7 +111,9 @@ public: vector searchRange(vector& nums, int target) { int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); - if (l == r) return {-1, -1}; + if (l == r) { + return {-1, -1}; + } return {l, r - 1}; } }; @@ -265,26 +216,28 @@ var searchRange = function (nums, target) { ```php class Solution { /** - * @param integer[] $nums - * @param integer $target - * @return integer[] + * @param Integer[] $nums + * @param Integer $target + * @return Integer[] */ - function searchRange($nums, $target) { - $min = -1; - $max = -1; - foreach ($nums as $key => $value) { - if ($value == $target) { - if ($min == -1) { - $min = $key; - } - - if ($key > $max) { - $max = $key; + $search = function ($x) use ($nums) { + $left = 0; + $right = count($nums); + while ($left < $right) { + $mid = intdiv($left + $right, 2); + if ($nums[$mid] >= $x) { + $right = $mid; + } else { + $left = $mid + 1; } } - } - return [$min, $max]; + return $left; + }; + + $l = $search($target); + $r = $search($target + 1); + return $l === $r ? [-1, -1] : [$l, $r - 1]; } } ``` diff --git a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md index b66b0fcdcf80d..491b8072d5031 100644 --- a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md +++ b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md @@ -52,60 +52,9 @@ tags: ### Solution 1: Binary Search -We can perform binary search twice to find the left and right boundaries respectively. +We can perform two binary searches to find the left boundary and the right boundary. -The time complexity is $O(\log n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$. - -Below are two general templates for binary search: - -Template 1: - -```java -boolean check(int x) { -} - -int search(int left, int right) { - while (left < right) { - int mid = (left + right) >> 1; - if (check(mid)) { - right = mid; - } else { - left = mid + 1; - } - } - return left; -} -``` - -Template 2: - -```java -boolean check(int x) { -} - -int search(int left, int right) { - while (left < right) { - int mid = (left + right + 1) >> 1; - if (check(mid)) { - left = mid; - } else { - right = mid - 1; - } - } - return left; -} -``` - -When doing binary search problems, you can follow the following routine: - -1. Write out the loop condition $left < right$; -2. Inside the loop, you might as well write $mid = \lfloor \frac{left + right}{2} \rfloor$ first; -3. According to the specific problem, implement the $check()$ function (sometimes the logic is very simple, you can not define $check$), think about whether to use $right = mid$ (Template $1$) or $left = mid$ (Template $2$); - - If $right = mid$, then write the else statement $left = mid + 1$, and there is no need to change the calculation of $mid$, that is, keep $mid = \lfloor \frac{left + right}{2} \rfloor$; - - If $left = mid$, then write the else statement $right = mid - 1$, and add +1 when calculating $mid$, that is, $mid = \lfloor \frac{left + right + 1}{2} \rfloor$; -4. When the loop ends, $left$ equals $right$. - -Note that the advantage of these two templates is that they always keep the answer within the binary search interval, and the value corresponding to the end condition of the binary search is exactly at the position of the answer. For the case that may have no solution, just check whether the $left$ or $right$ after the binary search ends satisfies the problem. +The time complexity is $O(\log n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$. @@ -152,7 +101,9 @@ public: vector searchRange(vector& nums, int target) { int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); - if (l == r) return {-1, -1}; + if (l == r) { + return {-1, -1}; + } return {l, r - 1}; } }; @@ -255,26 +206,28 @@ var searchRange = function (nums, target) { ```php class Solution { /** - * @param integer[] $nums - * @param integer $target - * @return integer[] + * @param Integer[] $nums + * @param Integer $target + * @return Integer[] */ - function searchRange($nums, $target) { - $min = -1; - $max = -1; - foreach ($nums as $key => $value) { - if ($value == $target) { - if ($min == -1) { - $min = $key; - } - - if ($key > $max) { - $max = $key; + $search = function ($x) use ($nums) { + $left = 0; + $right = count($nums); + while ($left < $right) { + $mid = intdiv($left + $right, 2); + if ($nums[$mid] >= $x) { + $right = $mid; + } else { + $left = $mid + 1; } } - } - return [$min, $max]; + return $left; + }; + + $l = $search($target); + $r = $search($target + 1); + return $l === $r ? [-1, -1] : [$l, $r - 1]; } } ``` diff --git a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.cpp b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.cpp index 21673df24de5a..dfbeed6c544a0 100644 --- a/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.cpp +++ b/solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/Solution.cpp @@ -3,7 +3,9 @@ class Solution { vector searchRange(vector& nums, int target) { int l = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); int r = lower_bound(nums.begin(), nums.end(), target + 1) - nums.begin(); - if (l == r) return {-1, -1}; + if (l == r) { + return {-1, -1}; + } return {l, r - 1}; } -}; \ No newline at end of file +};