首页 > 其他 > 详细

Search Insert Position

时间:2015-04-14 07:16:42      阅读:83      评论:0      收藏:0      [点我收藏+]

二分,多设立一个返回条件变可以继承二分法

ublic class Solution {
    public int searchInsert(int[] A, int target) {
        
        int l = 0, r = A.length-1;
        while(l<=r){
            int m = (l+r)/2;
            if(A[m]==target) return m;
            if(m+1<A.length && A[m]< target && A[m+1]>target){
                    return m+1;
            }
            if(A[m]< target){
                l = m+1;
            }else
                r =m-1;
        }
        return l;
    }
}

参考 http://fisherlei.blogspot.com/2013/01/leetcode-search-insert-position.html

Search Insert Position

原文:http://www.cnblogs.com/jiajiaxingxing/p/4423897.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!