1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
public class SearchInsertPosition { public int searchInsert(int[] nums, int target) { if (nums.length == 0 || target <= nums[0]) { return 0; } if (target > nums[nums.length - 1]) { return nums.length; } for (int i = 1; i < nums.length; i++) { if (target == nums[i]) { return i; } if (target > nums[i - 1] && target < nums[i]) { return i; } } return 1; } }
|