LeetCode解题报告--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,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
点击解题:Search Insert Position

分析:从给定排好顺序的数组,找出给定目标数字下标,存在则返回下标,不存在则返回目标数应该插入的位置下标。
提供两个可行思路:
1).二分查找
a.存在情况,就是经典二分查找问题
b.不存在情况,加个判别条件 nums[mid] < target && nums[mid + 1] > target 原因在于,如果存在target,则nums[mid]与target的关系必然存在三种 “>”,”<”和”==”。
2).双指针法
双指针法判断条件多,基本原理是left,rigth双指针分别指向nums首末元素,从左右两边分别开始判断元素(nums[left],nums[right] )与target的关系,则如果存在target,则nums[left(right)]与target的关系必然存在三种 “>”,”<”和”==”。以此为判断标准,就可找到index

至于暴力法也是可以求解,但会超时,我没试过,虽然题目没时间复杂度限制,但是应该是不允许。
盖提与Search for range思路相似,具体可参考该篇博客
改题是二分查找的变形题目,会二分查找,就迎刃而解。

Java代码 Accepted:
Binary Search:

public class Solution {
    public int searchInsert(int[] nums, int target) {

        //Binary Search
        int left = 0;
        int right = nums.length - 1;

        while(left <= right){
            int middle = (left + right) / 2;

            if(nums[middle] == target)
                return middle;
            else if(nums[middle] < target){
                left = middle + 1;
            }else if(nums[middle] > target){
                right = middle - 1;
            }else if(nums[middle] < target && nums[middle + 1] > target){
                return middle;
            }

        }
         return left;
    }
}

Double Points Search:

public class Solution {
    public int searchInsert(int[] nums, int target) {

        //double points search
        int left = 0;
        int right = nums.length - 1;
        //flag for whether nums[left] > target or nums[right] < target
        int flag = 0;
        //save index of target
        int index = 0;
        while(left <= right){
            //when nums's length == 1
            if(left == right){
                if(nums[left] > target)
                    return left;
                else if(nums[left] < target)
                    return left + 1;
                else
                    return left;
            }
            //find target,then save left to index
            else if(nums[left] == target){
                index = left;
                break;
            }
            //find target,then save right to index
            else if(nums[right] == target){
                index = right;
                break;
            }
            //when nums[left] < target, if nums[left + 1] > target, target not exsist
           else if(nums[left] < target){
                left ++;
                flag = 1;
            }
            else if(flag == 1 && nums[left] > target){
                index = left;
                break;
            }
            //when nums[right] > target, if nums[right - 1] < target, target not exsist
            else if(nums[right] > target){
                right --;
                flag = 1;
            }
            else if(flag == 1 && nums[right] < target){
                index = right;
                break;
            }
        }
        return index;
    }
}

Python 代码:
Binary Search:

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left = 0
        right = len(nums) - 1

        while(left <= right):
            mid = (left + right) / 2
            if(nums[mid] == target):
                return mid
            elif(nums[mid] < target):
                left = mid + 1
            elif(nums[mid] > target):
                right = mid - 1
            elif(nums[mid] < target and nums[mid + 1] < target):
                return mid + 1
        return left
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值