当前位置:网站首页>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
边栏推荐
- [jailhouse article] jailhouse hypervisor
- A new micro ORM open source framework
- Software test -- 0 sequence
- Talking about JVM (frequent interview)
- Sword finger offer 09 Implementing queues with two stacks
- CF1634E Fair Share
- lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- 卷积神经网络简介
- Fried chicken nuggets and fifa22
- Simple knapsack, queue and stack with deque
猜你喜欢
随机推荐
Time of process
Full Permutation Code (recursive writing)
剑指 Offer 04. 二维数组中的查找
卷积神经网络——卷积层
[practical skills] technical management of managers with non-technical background
Hang wait lock vs spin lock (where both are used)
F - Two Exam(AtCoder Beginner Contest 238)
浅谈JVM(面试常考)
Chapter 6 data flow modeling - after class exercises
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
[practical skills] how to do a good job in technical training?
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
On-off and on-off of quality system construction
“磐云杯”中职网络安全技能大赛A模块新题
sync.Mutex源码解读
API related to TCP connection
Daily question 2013 Detect square
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
How many checks does kubedm series-01-preflight have
26、 File system API (device sharing between applications; directory and file API)