Skip to content

Commit

Permalink
Merge pull request youngyangyang04#792 from Jerry-306/patch-20
Browse files Browse the repository at this point in the history
修改 0704 二分查找 JavaScript版本 左闭右闭区间 解法
  • Loading branch information
youngyangyang04 authored Sep 29, 2021
2 parents a608847 + 7f75ee1 commit 93a40f9
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions problems/0704.二分查找.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,16 @@ func search(nums []int, target int) int {
* @param {number} target
* @return {number}
*/
/**
var search = function(nums, target) {
let left = 0, right = nums.length;
// 使用左闭右开区间 [left, right)
while (left < right) {
let left = 0, right = nums.length - 1;
// 使用左闭右闭区间
while (left <= right) {
let mid = left + Math.floor((right - left)/2);
if (nums[mid] > target) {
right = mid; // 去左区间寻找
right = mid - 1; // 去左面闭区间寻找
} else if (nums[mid] < target) {
left = mid + 1; // 去右区间寻找
left = mid + 1; // 去右面闭区间寻找
} else {
return mid;
}
Expand Down

0 comments on commit 93a40f9

Please sign in to comment.