二分法查找,如果找到就返回索引,找不到就返回插入该数后的坐标

public static int searchInsert(int[] nums, int target) {
        int sta = 0,fin = nums.length,mid;
        while (sta < fin)
        {
            mid = (sta + fin )/2;
            if(nums[mid] < target)
                sta = mid +1;
            else
                fin = mid;
        }
        return sta;
    }

 

posted @ 2017-07-03 17:09  stAr_1  阅读(290)  评论(0编辑  收藏  举报