Jan 16 - Search Insert Position; Array; Binary Search; Iteration&Recursion;---Iteration再补上

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

Jan 16 - Search Insert Position; Array; Binary Search; Iteration&Recursion;---Iteration再补上的相关文章

35. Search Insert Position (Array)

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. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

LeetCode 5题 --Array+Binary search

这5道题都是array的binary search就可以搞定了 分别是leetcode(35)--Search Insert Position  leetcode(33)--Search in Rotated Sorted Array  leetcode(81)--Search in Rotated Sorted Array II  leetcode(34)--Search for a Range  leetcode(74)--Search a 2D Matrix 一:leetcode(35)-

Search Insert Position (LeetCode)

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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

【leetcode】Search Insert Position

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. Here are few examples.[1,3,5,

LeetCode: Search Insert Position 解题报告

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. Here are few examples.[1,3,5,

【LeetCode】Search Insert Position (2 solutions)

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. Here are few examples.[1,3,5,

leetcode_35题——Search Insert Position(二分查找)

Search Insert Position Total Accepted: 56150 Total Submissions: 158216My Submissions Question Solution 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

Search Insert Position leetcode java

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. Here are few examples.        [1,3,5,6], 5 → 2      

[LC]35题 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. Example 1: Input: [1,3,5,6], 5Output: 2Example