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.
Example 1:
Input: [1,3,5,6], 5 Output: 2
Example 2:
Input: [1,3,5,6], 2 Output: 1
Example 3:
Input: [1,3,5,6], 7 Output: 4
Example 1:
Input: [1,3,5,6], 0
Output: 0
/** * Created by lxw, liwei4939@126.com on 2018/3/6. */ public class L035_Search_Insert_Position { public int searchInsert(int[] nums, int target){ int lo = 0, hi = nums.length - 1; while (lo <= hi){ int mid = lo + (hi - lo) / 2; if (nums[mid] == target){ return mid; } else if (nums[mid] > target){ hi = mid - 1; } else { lo = mid + 1; } } return lo; } public static void main(String[] args){ L035_Search_Insert_Position tmp = new L035_Search_Insert_Position(); int[] arr = new int[]{1, 3, 5, 6}; System.out.println(tmp.searchInsert(arr, 7)); } }