leetcode 35. Search Insert Position-二分查找|递归|非递归

原题链接:35. Search Insert Position

【思路-Java、Python】——非递归实现

采用二分查找,对于二分查找需要仔细思考,有时候判断条件多一个或少一个“=”,可能会得不到正确结果。

对于二分查找分为:可以分为能在数组中找到结果的、不能找到结果的两种情况。找不到结果的情况分为,比最小值还小,比最大值还大,在最大值和最小值之间3中情况。这里的处理办法是:

1. ①处while 循环中的条件是 left < right,而不是 left <= right。

2. ②处 right = mid,而不是 right=mid-1,否则可能造成 right < left,例如 nums=[1,3], target=0,最后 left=0, right=-1

这样的处理会使得最后不管是找得到结果还是找不到结果,都会得到,left=right。利用这一可靠条件,最后加上③的判断就可以得到结果。

有些人可能会有疑问,那我将④改为 if (nums[mid] > target) right = mid - 1,同时将③改为 left = mid 可以吗?答案是否定的。例如nums=[1,3],target=1就会造成超时,究其原因,⑤采用下取整。可能陷入死循环的怪圈:

public class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left < right) {  //①
            int mid = left + (right-left)/2;  //⑤
            if (nums[mid] < target) left = mid + 1;  //④
            else right = mid;  //②
        }
        return nums[left] >= target ? left : left+1;  //③
    }
}
62 / 62  test cases passed. Runtime: 0 ms  Your runtime beats 19.33% of javasubmissions.
class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        left,right = 0,len(nums)-1
        while left < right :
            mid = (left+right)/2;
            if nums[mid] < target : left = mid+1
            else : right = mid-1
        return left if nums[left] >= target else left+1
62 / 62  test cases passed. Runtime: 56 ms  Your runtime beats 5.95% of pythonsubmissions.

【思路-Java、Python】——递归实现

非递归的实现了,那么递归的基本思想也和非递归十分类似,这里就不再赘述了:

public class Solution {
    public int searchInsert(int[] nums, int target) {
        return binarySearch(nums, target, 0, nums.length-1);
    }
    private int binarySearch(int[] nums, int target, int left, int right) {
        if(left >= right) return nums[left] >= target ? left : left+1;
        int mid = left + (right-left) /2;
        if(nums[mid] < target) return binarySearch(nums, target, left+1, right);
        else return binarySearch(nums, target, left, right-1);
    }
}
62 / 62  test cases passed. Runtime: 2 ms  Your runtime beats 1.32% of javasubmissions.

class Solution(object):
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        def binarySearch(nums,target,left,right) :
            if left >= right : return left if nums[left] >= target else left+1
            mid = (left+right) / 2
            if nums[mid] < target : return binarySearch(nums,target,mid+1,right)
            else : return binarySearch(nums,target,left,mid)
        return binarySearch(nums,target,0,len(nums)-1)
62 / 62  test cases passed. Runtime: 68 ms  Your runtime beats 0.93% of pythonsubmissions.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值