博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode]81. Search in Rotated Sorted Array II
阅读量:5142 次
发布时间:2019-06-13

本文共 2312 字,大约阅读时间需要 7 分钟。

  • 本题难度: Medium
  • Topic: Binary Search

    类似

    Description

  1. Search in Rotated Sorted Array II
    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).

You are given a target value to search. If found in the array return true, otherwise return false.

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
Follow up:

This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.

Would this affect the run-time complexity? How and why?

我的的代码

class Solution:    def search(self, A: 'List[int]', target: 'int') -> 'bool':        # write your code here        if A  == []:            return False        l = 0        r = len(A)-1#注意!        f = -1 # breakpoint        if len(A) == 1:            return A[0] == target        for i in range(1,len(A)):            if A[i-1]>A[i]:                f = i        if f == -1:            l = 0            r = len(A) - 1        else:            if target>=A[0]:                l = 0                r = f-1            else:                l = f                r = len(A)-1        while(l<=r):            m = (l+r)//2            if A[m] == target:                return True            else:                if A[m]>target:                    r = m-1                else:                    l = m+1        return False

思路

如果遇到[2,2,2,2,0,2,2]这类情况,没想好怎么做,所以

  • 时间复杂度: O(log(n))

别人的代码

def search(self, nums, target):    l, r = 0, len(nums)-1    while l <= r:        mid = l + (r-l)//2        if nums[mid] == target:            return True        while l < mid and nums[l] == nums[mid]: # tricky part            l += 1        # the first half is ordered        if nums[l] <= nums[mid]:            # target is in the first half            if nums[l] <= target < nums[mid]:                r = mid - 1            else:                l = mid + 1        # the second half is ordered        else:            # target is in the second half            if nums[mid] < target <= nums[r]:                l = mid + 1            else:                r = mid - 1    return False

思路

如果遇到[2,2,2,2,0,2,2]这类情况,没想好怎么做,所以

  • 时间复杂度: average : O(log(n)) worst O(n)

转载于:https://www.cnblogs.com/siriusli/p/10358577.html

你可能感兴趣的文章
洛谷 P3237 [HNOI2014]米特运输
查看>>
Attributes.Add用途与用法
查看>>
JavaScript面向对象初探——封装和继承
查看>>
L2-001 紧急救援 (dijkstra+dfs回溯路径)
查看>>
javascript 无限分类
查看>>
spring IOC装配Bean(注解方式)
查看>>
[面试算法题]有序列表删除节点-leetcode学习之旅(4)
查看>>
SpringBoot系列五:SpringBoot错误处理(数据验证、处理错误页、全局异常)
查看>>
kubernetes_book
查看>>
OpenFire 的安装和配置
查看>>
ZJOI2018游记Round1
查看>>
侧边栏广告和回到顶部
查看>>
https://blog.csdn.net/u012106306/article/details/80760744
查看>>
ios应用版本号设置规则
查看>>
海上孤独的帆
查看>>
error: more than one device and emulator 问题解决
查看>>
Java基础:容器
查看>>
YUV摘要格式
查看>>
【方法2】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
查看>>
C# CheckedListBox控件的使用方法
查看>>