标签:target public ret sort while dex search 个人 insert
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;
}
标签:target public ret sort while dex search 个人 insert
原文地址:http://www.cnblogs.com/KingIceMou/p/7825626.html