Recursion:
代码:
public class Solution { public int searchInsert(int[] nums, int target) { int len = nums.length; if(len == 0) return 0; return findPosition(nums, 0, len-1, target); } public int findPosition(int[] nums, int start, int end, int target){ if(target > nums[end]) return end+1; if(target < nums[start]) return start; int mid = (start + end)/2; int num_mid = nums[mid]; if(target == num_mid) return mid; if(target > num_mid) return findPosition(nums, mid+1, end, target); else return findPosition(nums, start, mid-1, target); } }
Iteration的之后再补上
时间: 2024-11-16 08:44:20