当前位置:网站首页>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边栏推荐
- 【云原生】微服务之Feign自定义配置的记录
- Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
- R语言【数据集的导入导出】
- Implement an iterative stack
- 7. Processing the input of multidimensional features
- Over fitting and regularization
- ALU逻辑运算单元
- YOLOv5-Shufflenetv2
- R language [import and export of dataset]
- Daily question 2013 Detect square
猜你喜欢

挂起等待锁 vs 自旋锁(两者的使用场合)

Sword finger offer 04 Search in two-dimensional array

Sword finger offer 58 - ii Rotate string left

EOJ 2021.10 E. XOR tree

CF1637E Best Pair

智慧工地“水电能耗在线监测系统”

Sword finger offer 09 Implementing queues with two stacks

剑指 Offer 53 - II. 0~n-1中缺失的数字

YOLOv5-Shufflenetv2

Sword finger offer 05 Replace spaces
随机推荐
A new micro ORM open source framework
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Codeforces Round #716 (Div. 2) D. Cut and Stick
Sword finger offer 53 - I. find the number I in the sorted array
Control unit
Implement a fixed capacity stack
PC寄存器
Solution to the palindrome string (Luogu p5041 haoi2009)
sync.Mutex源码解读
CF1634 F. Fibonacci Additions
Acwing 4300. Two operations
Simple knapsack, queue and stack with deque
Haut OJ 1401: praise energy
Maximum number of "balloons"
Wazuh開源主機安全解决方案的簡介與使用體驗
Palindrome (csp-s-2021-palin) solution
Time complexity and space complexity
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Hang wait lock vs spin lock (where both are used)
Fried chicken nuggets and fifa22