1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public int search(int[] nums, int target) { if (nums.length == 0) return -1; if (nums.length == 1) return nums[0] == target ? 0 : -1; int index = -1; int lfet = 0; int right = nums.length - 1; return search(lfet, right, nums, target); } public int search(int left, int right, int[] nums, int target) { if (left > right) { return -1; } int center = (left + right) / 2; if (nums[center] == target) { return center; } else if (nums[center] > target) { return search(left, center - 1, nums, target); } else { return search(center + 1, right, nums, target); } } }
|