标签:
Search Insert Position
问题:
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 class Solution { public int searchInsert(int[] A, int target) { if( A == null || A.length == 0) return -1; int left = 0; int right = A.length - 1; while(left <= right) { int mid = (left + right)/2; if(A[mid] == target) return mid; else if(A[mid] > target) right = mid - 1; else left = mid + 1; } return left; } }
学习之处:
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4313581.html