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岁的励志奶奶尚且如此辛苦

  • 相关阅读:
    完整版excel上传导入读写批量数据并将反馈结果写入远程exel中
    将数据写入已有的excel文件
    微服务项目启动问题
    通过POI实现上传EXCEL的批量读取数据写入数据库
    [转] VLAN原理详解
    [转载]git tag — 标签相关操作
    [转载]SQLite3性能优化
    [转载]提升SQLite数据插入效率低、速度慢的方法
    [转载]sqlite3遇到database is locked问题的完美解决
    一个Linux下C线程池的实现(转)
  • 原文地址:https://www.cnblogs.com/wangyuxia/p/14643930.html
Copyright © 2011-2022 走看看