2020-12-25Today's Niuke and Leetcode== Binary search of ordered array with repeated numbers + rotated ordered array to find target value + minimum number of rotated array

Source: Link: https://www.nowcoder.com/practice/7bc4a1c7c371425d9faa9d1b511fe193?tpId=188&&tqId=36844&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
Statement: If I violate Anyone’s rights, please contact me and I will delete it.
Welcome experts to spray me

Title description Please implement binary search for ordered arrays with repeated numbers.

The output is the first position in the array that is greater than or equal to the search value. If there is no such number in the array, the length of the output array is increased by one.

Example 1
Input 5,4,[1,2,4,4,5]
Return value 3
Output position is calculated from 1

Code, dichotomous details are the devil
import java.util.*; 
public class Solution {
    /**
     * 二分查找
     * @param n int整型 数组长度
     * @param v int整型 查找值
     * @param a int整型一维数组 有序数组
     * @return int整型
     */
    public int upper_bound_ (int n, int v, int[] a) { 
        //数组是排序好的, 那么判断最后一个值大于查找值,那么没有查找的意义了
        if(a[n-1] < v) return n+1;
        
        int left = 0, right = a.length;
        
        while(left < right){  
            int mid = left + (right - left)/2;
            if(a[mid] > v) right = mid;
            else if(a[mid] < v) left = mid+1; 
            //这里是应对重复值得思路
            //重复值,就顺序查找
            else{ //a[mid] == v
                while(mid >=0 && a[mid]==v) mid--;
                //为什么是+2???因为此时mid指向小于v的最后一个值,
                //mid+1指向等于v的第一个值,mid+1+1才是 符合“输出位置从1开始计算 ”的结果
                return mid+2;
            }
        }
        //输出位置从1开始计算 所以加1
        return left + 1;
    }
}
The title describes the rotated ordered array to find the target value

Given an ordered array that has been rotated, you don’t know how much the array has been rotated in advance
(for example, 0 1 2 4 5 6 7 may become 4 5 6 7 0 1 2).
Search for the given target value in the array , If it can be found in the array, return its index, otherwise return -1.
Assume that there are no duplicates in the array.
Example 1
input [1], 0
return value -1
Example 2
input [3,
2 , 1], 1 return value 2

Code points: 1 Arr[mid]=target 2: The right half is in order 3: The left half is in order

After evenly dividing the rotating array, one side is always ordered, such as:

10 11 12 13 14 15 1 2 3
10 11 15 1 2 3 4 5 6 7 8 After
we locate the ordered side, compare the left and right boundaries of the target and the ordered sub-array, and then we can search for the left side or the right side decision making.

import java.util.*; 
public class Solution {
    /**
     * 
     * @param A int整型一维数组 
     * @param target int整型 
     * @return int整型
     */
    public int search (int[] A, int target) {
        // write code here
        int left = 0, right = A.length-1;
        while(left <= right){
            int mid = left + (right-left)/2;
            if(A[mid] == target) return mid;
            else if(A[mid] < A[right]){ //右半部有序
                if(A[mid] < target && target <= A[right]) left = mid+1;
                else right = mid - 1;
            }else{ //左半部有序
                if(target < A[mid] && A[left]<= target) right = mid-1;
                else left = mid + 1;
            }
        } 
        //不要想太多,找不到就返回-1
        return -1;
    }
}  
Title Description Sword refers to Offer 11. Rotate the smallest number of the array

Link: https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof

Moving the first few elements of an array to the end of the array is called the rotation of the array. Input a rotation of an ascending array, and output the smallest element of the rotated array. For example, the array [3,4,5,1,2] is a rotation of [1,2,3,4,5], and the minimum value of the array is 1.
Example 1:
Input: [3,4,5,1,2]
Output: 1
Example 2:
Input: [2,2,2,0,1]
Output: 0

Dichotomy of code details

We consider the last element x in the array: the elements to the right of the minimum must all have their values ​​less than or equal to x;
and the elements to the left of the minimum must all have their values ​​greater than or equal to x. Therefore, we can find the minimum value by binary search based on this property.

  • The first case is numbers[pivot]<numbers[high]. This shows that numbers[pivot] is the element to the right of the minimum value, so we can ignore the right half of the binary search interval.
  • The second case is numbers[pivot]>numbers[high]. This shows that numbers[pivot] is the element to the left of the minimum value, so we can ignore the left half of the binary search interval.
  • The third case is numbers[pivot]==numbers[high]. Due to the existence of repeated elements, we cannot be sure whether numbers[pivot] is to the left or right of the minimum value.
class Solution {
    public int minArray(int[] numbers) {
        int left = 0, right = numbers.length-1;
        while(left <= right){
            int mid = left + (right - left)/2;
            if(numbers[mid] < numbers[right]){
            //这里为什么不是right-1??因为mid这个值是小值,可能是我的所求,所以到带入下一次比较
                right = mid;
            }else if(numbers[mid] > numbers[right]){
                left = mid+1;
            }else{
                right--;
            }
        }
        return numbers[left];
        
    }
}
Two points summarized by the great god, various details and various applications

Detailed explanation of binary search algorithm https://www.cnblogs.com/kyoner/p/11080078.html

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/111680807