You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/0700-0799/0704.Binary Search/README_EN.md
+74-79Lines changed: 74 additions & 79 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,16 +56,16 @@ tags:
56
56
57
57
### Solution 1: Binary Search
58
58
59
-
We define the left boundary of the binary search as $left=0$, and the right boundary as $right=n-1$.
59
+
We define the left boundary $l=0$ and the right boundary $r=n-1$ for binary search.
60
60
61
-
In each iteration, we calculate the middle position $mid=(left+right)/2$, and then compare the size of $nums[mid]$ and $target$:
61
+
In each iteration, we calculate the middle position $\text{mid}=(l+r)/2$, then compare the size of $\text{nums}[\text{mid}]$ and $\text{target}$.
62
62
63
-
- If $nums[mid] \geq target$, it means that $target$ is in the interval $[left, mid]$, so we update $right$ to $mid$;
64
-
- Otherwise, $target$ is in the interval $[mid+1, right]$, so we update $left$ to $mid+1$.
63
+
- If $\text{nums}[\text{mid}] \geq \text{target}$, it means $\text{target}$ is in the left half, so we move the right boundary $r$ to $\text{mid}$;
64
+
- Otherwise, it means $\text{target}$ is in the right half, so we move the left boundary $l$ to $\text{mid}+1$.
65
65
66
-
When $left \geq right$, we check if $nums[left]$ equals $target$. If it does, we return $left$, otherwise, we return $-1$.
66
+
The loop ends when $l<r$, at this point $\text{nums}[l]$ is the target value we are looking for. If $\text{nums}[l]=\text{target}$, return $l$; otherwise, return $-1$.
67
67
68
-
The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.
68
+
The time complexity is $O(\log n)$, where $n$ is the length of the array $\text{nums}$. The space complexity is $O(1)$.
69
69
70
70
<!-- tabs:start -->
71
71
@@ -74,31 +74,31 @@ The time complexity is $O(\log n)$, where $n$ is the length of the array $nums$.
0 commit comments