Search Insert Position

来源:互联网 发布:三星9108软件下载 编辑:程序博客网 时间:2024/04/29 18:22

二分搜索

public class Solution {    public int searchInsert(int[] A, int target) {        int low = 0, high = A.length-1;        if(high == -1) return 0;                while(low <= high){            int mid = low + ((high - low)>>1);            if(A[mid] == target) return mid;                        if(A[mid] > target) high = mid - 1;            else low = mid + 1;        }        return low;    }}


0 0
原创粉丝点击