当前位置:网站首页>Binary search template
Binary search template
2022-07-05 05:42:00 【A big pigeon】
author :labuladong
link :https://leetcode-cn.com/problems/binary-search/solution/er-fen-cha-zhao-xiang-jie-by-labuladong/
source : Power button (LeetCode)
Give a binary search template , Include : Binary search target value , Left boundary , Right border .
(Java and Python edition )
It's easy to find the target value directly ,
nums[mid]<target, Compress left left = mid +1
nums[mid]>target, Compress right right = mid -1
nums[mid]==target , return mid
Find the first two parts of the left boundary, similar to , but nums[mid]==target Continue to compress the right boundary , Lock left boundary , Finally, return to the left boundary . Pay attention to handling cross-border situations .
Finding the right boundary is the same as .
int binary_search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else if(nums[mid] == target) {
// Go straight back to
return mid;
}
}
// Go straight back to
return -1;
}
int left_bound(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] == target) {
// Don't go back , Lock the left border
right = mid - 1;
}
}
// Finally, check left The situation of crossing the border
if (left >= nums.length || nums[left] != target)
return -1;
return left;
}
int right_bound(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] > target) {
right = mid - 1;
} else if (nums[mid] == target) {
// Don't go back , Lock the right border
left = mid + 1;
}
}
// Finally, check right The situation of crossing the border
if (right < 0 || nums[right] != target)
return -1;
return right;
}
class Solution:
def binary_search(self,nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
elif nums[mid] == target:
return mid
return -1
def left_bound(self,nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
elif nums[mid] == target:
right = mid -1
if left >= len(nums) or nums[left] != target:
return -1
return left
def right_bound(self,nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] < target:
left = mid + 1
elif nums[mid] > target:
right = mid - 1
elif nums[mid] ==target:
left = mid + 1
if right < 0 or nums[right] != target:
return -1
return right
边栏推荐
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
- 从Dijkstra的图灵奖演讲论科技创业者特点
- 挂起等待锁 vs 自旋锁(两者的使用场合)
- Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
- Haut OJ 2021 freshmen week II reflection summary
- Hang wait lock vs spin lock (where both are used)
- Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
- Fried chicken nuggets and fifa22
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
猜你喜欢
【实战技能】如何做好技术培训?
CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
智慧工地“水电能耗在线监测系统”
【实战技能】非技术背景经理的技术管理
Personal developed penetration testing tool Satania v1.2 update
Acwing 4300. Two operations
Web APIs DOM node
游戏商城毕业设计
剑指 Offer 05. 替换空格
Typical use cases for knapsacks, queues, and stacks
随机推荐
软件测试 -- 0 序
Use of room database
【Jailhouse 文章】Look Mum, no VM Exits
Annotation and reflection
Implement an iterative stack
Chapter 6 data flow modeling - after class exercises
Sword finger offer 58 - ii Rotate string left
Daily question 2013 Detect square
2022年贵州省职业院校技能大赛中职组网络安全赛项规程
A new micro ORM open source framework
Dynamic planning solution ideas and summary (30000 words)
Demonstration of using Solon auth authentication framework (simpler authentication framework)
26、 File system API (device sharing between applications; directory and file API)
Transform optimization problems into decision-making problems
How many checks does kubedm series-01-preflight have
Time complexity and space complexity
剑指 Offer 05. 替换空格
Control Unit 控制部件
A problem and solution of recording QT memory leakage
The number of enclaves