当前位置:网站首页>Binary search and its extension
Binary search and its extension
2022-07-19 06:40:00 【hello,world_ yzs】
List of articles
Two points search
Basic binary search
leetcode Two points search
Be careful while The judgment in the loop and the value of the boundary :
stay int high = arr.length-1; When , It's equivalent to a closed interval at both ends [left, right]
high =mid-1;
while(low <= high)
public static int binary(int [] arr,int num) {
int low =0;
int high = arr.length-1;
while(low<=high) {
int mid =low +(high-low)/2;
if(arr[mid]>num) {
high =mid-1;
}else if(arr[mid]<num) {
low =mid+1;
}else if(arr[mid]==num) {
return mid;
}
}
return -1;
}
If
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length ;
while(left < right){
int mid = left + (right - left)/2;
if(nums[mid] < target)
left = mid + 1;
else if(nums[mid] > target)
right = mid;
else if(nums[mid] == target)
return mid;
}
return -1;
}
stay int high = arr.length; When , It's equivalent to a left closed right open interval [left, right)
high =mid;
while(low < high)
On the left
leetcode Search insert location
public int searchInsert(int[] nums, int target) {
int left = 0;
int 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)
right = mid - 1;
}
return left;
}
Returns if none exists -1.
public static int left_bound_fuyi(int [] arr,int num) {
//not find, return -1
int low =0;
int high = arr.length-1;
while(low<=high) {
int mid =low +(high-low)/2;
if(arr[mid]>num) {
high =mid-1;
}else if(arr[mid]<num) {
low =mid+1;
}else if(arr[mid]==num) {
high = mid-1;
}
}
if(low == arr.length)
return -1;
return arr[low]==num?low:-1;
}
On the right
If the element repeats , Return the element on the right , That is, less than or equal to its largest element
public static int right_bound1(int [] arr,int num) {
//high = arr.length;
int low =0;
int high = arr.length;
while(low<high) {
int mid =low +(high-low)/2;
if(arr[mid]>num) {
high =mid;
}else if(arr[mid]<num) {
low =mid+1;
}else if(arr[mid]==num) {
low =mid+1;
}
}
return low-1;
}
public static int right_bound(int [] arr,int num) {
//high = arr.length - 1;
int low =0;
int high = arr.length - 1;
while(low <= high) {
int mid =low +(high-low)/2;
if(arr[mid]>num) {
high = mid - 1;
}else if(arr[mid]<num) {
low = mid+1;
}else if(arr[mid]==num) {
low = mid+1;
}
}
return high;//return low - 1;
}
return -1
public static int right_bound_fuyi(int [] arr,int num) {
//arr Whirling Disaster Kun � num Yan ㄥ There are many insects in it Suzhuan torture
int low =0;
int high = arr.length;
while(low<high) {
int mid =low +(high-low)/2;
if(arr[mid]>num) {
high =mid;
}else if(arr[mid]<num) {
low =mid+1;
}else if(arr[mid]==num) {
low =mid+1;
}
}
if(low==0) return -1;
return arr[low-1]==num?(low-1):-1;
}
Closest to the element k It's worth
Find the closest k It's worth
1. Double pointer 

public List<Integer> findClosestElement_zhizhen(int[] arr,int k, int x){
int i =0;
int j =arr.length-1;
int len = arr.length;
while(len>k) {
if(Math.abs(arr[i]-x)>Math.abs(arr[j]-x))
i++;
else
j--;
len--;
}
List<Integer> res = new ArrayList<>();
for(int index= i;index<=j;index++) {
res.add(arr[index]);
}
return res;
}
public List<Integer> findClosestElement_binary(int[] arr,int k, int x){
/* * binarysearch Lock The cushion is used to incubate the touch bar ╂ Long Hao Tell me ч Pot � * [mid,mid+k] Turn the brush back � */
int i =0;
int j =arr.length-k;
while(i<j) {
int mid = i+(j-i)/2;
if(Math.abs(arr[mid]-x)>Math.abs(arr[mid+k]-x))//
i=mid+1;
else
j=mid;
}
List<Integer> res = new ArrayList<>();
for(int index= i;index<i+k;index++) {
res.add(arr[index]);
}
return res;
}
2. Two points search 
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int i =0;
int j =arr.length - k - 1;
while(i <= j) {
int mid = i+(j-i)/2;
if(Math.abs(arr[mid]-x)>Math.abs(arr[mid+k]-x))
i=mid+1;
else
j=mid - 1;
}
List<Integer> res = new ArrayList<>();
for(int index= i;index<i+k;index++) {
res.add(arr[index]);
}
return res;
}
Ordered two-dimensional matrix
Determine whether there is a certain number
/* Ideas
- Matrix is ordered , From the lower left corner , The number goes up and down , Increase the number to the right ,
- So start at the bottom left , When the number to be found is larger than the number in the lower left corner . Move right
- To find the number is less than the number in the lower left corner , Move upward
*/
public boolean Find(int target,int [][] array) {
int row=0;
int col=array[0].length-1;
while(row<=array.length-1&&col>=0){
if(target==array[row][col])
return true;
else if(target>array[row][col])
row++;
else
col--;
}
return false;
}
The first k Number
Two layer control :left=right, meanwhile count = k When output the desired value
Find less than or equal to mid When , From the first column of the last row, find , The idea of the same question
class Solution {
public int kthSmallest(int[][] matrix, int k) {
int left = matrix[0][0];
int n = matrix.length;
int right = matrix[n - 1][n - 1];
while(left < right) {
int mid = (left + right)/2;
int count = findCount(matrix, mid);
if(count < k)
left = mid + 1;
else
right = mid;
}
return left;
}
public int findCount(int[][] matrix, int mid) {
int n = matrix.length;
int i = n - 1;
int j = 0;
int count = 0;
// Each column is less than or equal to mid The number of
while(i >= 0 && j < n){
if(matrix[i][j] <= mid) {
count += i + 1;
j ++;
}else {
i --;
}
}
return count;
}
}
Two ordered arrays
Merge two ordered arrays
The second order of the ordered array k Number
The median of an ordered array
边栏推荐
- C language specifies how many days to display from the date
- [force buckle] realize stack with queue
- Quantum three body problem: an overview of numerical computation
- Preparation of blast Library of rust language from scratch (2) -- detailed explanation of Blas matrix format
- Part of the second Shanxi Network Security Skills Competition (Enterprise Group) WP (II)
- Robot stitching gesture recognition and classification
- No application for domain name SSL certificate under ports 80 and 443 (applicable to acme.sh and certbot)
- 2022/07/12 learning notes (day05) cycle
- [force buckle] copy the linked list with random pointer
- Dual tone sorting of CUDA and large arrays
猜你喜欢

吴恩达机器学习第8-9章

2022/07/10 group 5 Ding Shuai's study notes day03

Query of database (II)
![[force buckle] ring list II](/img/12/9cbc7081e2f7a87e77faf0e02a8c26.png)
[force buckle] ring list II

《PyTorch深度学习实践》-B站 刘二大人-day4

2022/07/09 group 5 Ding Shuai's study notes day02

锁

DSL implements metrics aggregation

Spot detection record

Solution: unable to load file c:\program files\ Because running scripts is forbidden on this system
随机推荐
From entering URL to displaying page
Depth first search (DFS for short)
[force buckle] realize queue with stack
使用候选选择从人类注视中学习视频显著性
C language specifies how many days to display from the date
Perceive the attention status of users on smart phones
Cygwin cooperates with listary to switch the current directory and quickly open it
山西省第二届网络安全技能大赛(企业组)部分赛题WP(四)
基于运动和视觉突出性的自我视频中的注意预测
No application for domain name SSL certificate under ports 80 and 443 (applicable to acme.sh and certbot)
从零开始的 Rust 语言 blas 库之预备篇(2)—— blas 矩阵格式详解
日常的眼睛接触检测使用无监督的注视目标发现
Set the index library structure, add suggestions that can be automatically completed to users, and turn some fields into collections and put them into suggestions
感知智能手機上用戶的關注狀態
Experiment 1 simple program design
Visual saliency based visual gaze estimation
工作中遇到的一些问题
通过VOR深度估计解决三维注视交互中的目标模糊问题
Local makefile compile other folder files specify obj directory
[force buckle] copy the linked list with random pointer