We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接
先来看下有序数组的二分查找。
const search = function(nums, target) { let start = 0 let end = nums.length - 1 while (start <= end) { const mid = start + ((end - start) >> 1) if (nums[mid] === target) return mid if (nums[mid] < target) { start = mid + 1 } else { end = mid - 1 } } return -1 }
nums[mid]
target
mid
nums[start]
start
end
const search = function(nums, target) { let start = 0 let end = nums.length - 1 while (start <= end) { const mid = start + ((end - start) >> 1) if (nums[mid] === target) return mid if (nums[mid] >= nums[start]) { if (target >= nums[start] && target < nums[mid]) { end = mid - 1 } else { start = mid + 1 } } else { if (target > nums[mid] && target <= nums[end]) { start = mid + 1 } else { end = mid - 1 } } } return -1 }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接
二分查找
先来看下有序数组的二分查找。
nums[mid]
与target
的关系来定位target
是在mid
的左边还是右边nums[mid]
与nums[start]
的大小,来定位mid
在左边还是右边target
,判断target
在mid
的左边还是右边,然后分别调整边界start
和end
The text was updated successfully, but these errors were encountered: