Leetcode 35. Search Insert Position--在有序数组中,查找目标值的插入位置。如果tar存在,则返回tar的下标;如果tar不存在,则返回插入tar的正确的下标

Posted 二十六画生的博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode 35. Search Insert Position--在有序数组中,查找目标值的插入位置。如果tar存在,则返回tar的下标;如果tar不存在,则返回插入tar的正确的下标相关的知识,希望对你有一定的参考价值。

Given a sorted array of distinct integers 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 must write an algorithm with O(log n) runtime complexity.

Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2

Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4

Constraints:

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums contains distinct values sorted in ascending order.
  • -104 <= target <= 104
/**
 * @Author you guess
 * @CreateTime 2018/11/12 20:28
 * @Desc 在有序数组中,查找目标值的插入位置。如果tar存在,则返回tar的下标;如果tar不存在,则返回插入tar的正确的下标。
 **/
public class Solution_35_SearchInsertPosition 


    /**
     * 2022-05-01
     * 最佳方法!
     * Tn=O(logn)
     * 思想:
     * 1 使用二分法查找,如果找到,则直接返回下标,'
     * 2 如果没找到,则nums[mid]要么比tar小,要么比tar大(此时nums[mid]是与tar最接近的值!)
     * 2.1 如果nums[mid]<tar,则直接返回mid+1,放在mid的下一位即可
     * 2.2 如果nums[mid]>tar,则直接返回mid,放在mid位即可
     * <p>
     * Runtime: 0 ms, faster than 100.00% of Java online submissions for Search Insert Position.
     * Memory Usage: 42.9 MB, less than 70.36% of Java online submissions for Search Insert Position.
     * <p>
     * [1,3],2 -> 1
     * [1,3],4 -> 2
     *
     * @param nums
     * @param target
     * @return
     */
    public int searchInsert(int[] nums, int target) 
//        这2个if不写也正确
//        if (nums[0] > target) 
//            return 0;
//         else if (nums[nums.length - 1] < target) 
//            return nums.length;
//        
        int low = 0;
        int high = nums.length - 1;
        int mid = 0;
        while (low <= high) 
            mid = (low + high) / 2;
            if (target == nums[mid]) 
                return mid;
             else if (nums[mid] < target) 
                low = mid + 1;
             else 
                // nums[mid] > target
                high = mid - 1;
            
        //while
        if (nums[mid] < target) 
            return mid + 1;
         else 
            // nums[mid] > target
            return mid;
        //else
    //searchInsert


    /**
     * Tn=O(n)
     * 2018/11/12
     *
     * @param nums
     * @param target
     * @return
     */
    public int searchInsert2(int[] nums, int target) 
        for (int i = 0; i < nums.length; i++) 
            if (target == nums[i]) 
                return i;
             else 
                if (target < nums[i])  //返回第一个比它大的元素的下标
                    return i;
                
            
        
        return nums.length;
    


    public static void main(String[] args) 
        Solution_35_SearchInsertPosition main = new Solution_35_SearchInsertPosition();
        System.out.println(main.searchInsert(new int[]1, 3, 5, 6, 7, 4));//2
//        System.out.println(main.searchInsert(new int[]1, 3, 5, 6, 7, 8));//5
//        System.out.println(main.searchInsert(new int[]1, 3, 5, 6, 7, 0));//0
//        System.out.println(main.searchInsert(new int[]1, 3, 2));//1
//        System.out.println(main.searchInsert(new int[]1, 3, 4));//2
//        System.out.println(main.searchInsert(new int[]1, 3, 0));//0
//        System.out.println(main.searchInsert(new int[]3, 5, 7, 9, 10, 8));
    


 

以上是关于Leetcode 35. Search Insert Position--在有序数组中,查找目标值的插入位置。如果tar存在,则返回tar的下标;如果tar不存在,则返回插入tar的正确的下标的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode]Binary Search-35. Search Insert Position

LeetCode 35. Search Insert Position

[Binary Search] Leetcode 35, 74

LeetCode OJ 35. Search Insert Position

[LeetCode]35. Search Insert Position

Leetcode-35 Search Insert Position