首页 > 其他 > 详细

LeetCode 2

时间:2017-11-13 12:47:38      阅读:27      评论:0      收藏:0      [点我收藏+]

标签:int   may   search   ray   leetcode   earch   insert   clas   array   

No1

  

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

个人:

public int searchInsert(int[] nums, int target) {
		for(int i=0;i<nums.length;i++){
			if(nums[i]>=target){
				return i;
			}
		}
		return nums.length;

	}

  优秀代码:

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

LeetCode 2

标签:int   may   search   ray   leetcode   earch   insert   clas   array   

原文:http://www.cnblogs.com/KingIceMou/p/7825626.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号