zoukankan      html  css  js  c++  java
  • Leetcode** 81. Search in Rotated Sorted Array II

    Description: There is an integer array nums sorted in non-decreasing order (not necessarily with distinct values).

    Before being passed to your function, nums is rotated at an unknown pivot index k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,4,4,5,6,6,7] might be rotated at pivot index 5 and become [4,5,6,6,7,0,1,2,4,4].

    Given the array nums after the rotation and an integer target, return true if target is in nums, or false if it is not in nums.

    Link: 81. Search in Rotated Sorted Array II

    Examples:

    Example 1:
    Input: nums = [2,5,6,0,0,1,2], target = 0
    Output: true
    
    Example 2:
    Input: nums = [2,5,6,0,0,1,2], target = 3
    Output: false
    
    Example 3:
    Input: nums = [1,0,1,1,1], target = 0
    Output: true

    思路: 这个题目是33的加强版,添加了重复的数字,但只要返回True, False. 如果按照33的源代码就会过不了example3,因为l == mid, mid == r,就没办法进入看到0.返回False。症结就在于数组断开的地方前后是重复的数字,一部分留在开头,另一部分留在了末尾,因为只要返回是否存在,所以l从不同与r的数开始,再开始正常的搜索。

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: bool
            """
            if not nums: return False
            n = len(nums)
            l, r = 0, n-1
            while l < r and nums[l] == nums[r]:
                l += 1
            while l <= r:
                mid = int((l+r)/2)
                if target == nums[mid]:
                    return True
                if nums[l] <= nums[mid]:
                    if target >= nums[l] and target <= nums[mid]:
                        r = mid - 1
                    else:
                        l = mid + 1 
                elif nums[mid] <= nums[r]:
                    if target >= nums[mid] and target <= nums[r]:
                        l = mid + 1
                    else:
                        r = mid - 1
            return False

    在这篇题解博客中,将找到l不同于r放在了while里面:

    class Solution(object):
        def search(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: bool
            """
            if not nums: return False
            n = len(nums)
            l, r = 0, n-1
            while l <= r:
                while l < r and nums[l] == nums[r]:
                    l += 1
                mid = int((l+r)/2)
                if target == nums[mid]:
                    return True
                if nums[l] <= nums[mid]:
                    if target >= nums[l] and target <= nums[mid]:
                        r = mid - 1
                    else:
                        l = mid + 1 
                elif nums[mid] <= nums[r]:
                    if target >= nums[mid] and target <= nums[r]:
                        l = mid + 1
                    else:
                        r = mid - 1
            return False

    日期: 2021-04-11 96岁的励志奶奶尚且如此辛苦

  • 相关阅读:
    Maximum Flow Exhaustion of Paths Algorithm
    ubuntu下安装java环境
    visualbox使用(二)
    vxworks一个超级奇怪的错误(parse error before `char')
    February 4th, 2018 Week 6th Sunday
    February 3rd, 2018 Week 5th Saturday
    February 2nd, 2018 Week 5th Friday
    February 1st, 2018 Week 5th Thursday
    January 31st, 2018 Week 05th Wednesday
    January 30th, 2018 Week 05th Tuesday
  • 原文地址:https://www.cnblogs.com/wangyuxia/p/14643930.html
Copyright © 2011-2022 走看看