题目来源


https://leetcode.com/problems/search-in-rotated-sorted-array-ii/

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

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

Write a function to determine if a given target is in the array.


题意分析


Input:

:type nums: List[int]
:type target: int

Output:

rtype: bool

Conditions:一个翻转的有序数组,数组元素可能重复,判断target是否在数组中


题目思路


关键就要区分边界,采用first表示下界,last表示上界,mid为中间点。如果mid为target,返回True;否则,判断mid,first,last是否相等,若相等则缩小搜索空间,之后判断target在哪个区间,判断条件为:1)target与nums[mid]的大小,target与nums[first]的大小。


AC代码(Python)

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

[LeetCode]题解(python):081 - Search in Rotated Sorted Array II的更多相关文章

  1. 【LeetCode】081. Search in Rotated Sorted Array II

    题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...

  2. Java for LeetCode 081 Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  3. 081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||

    这是 “搜索旋转排序数组”问题的跟进:如果数组元素允许重复,怎么办?这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?假设按照升序排序的数组在预先未知的某个关键点上旋转.(例如, 0 1 2 4 ...

  4. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  5. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  6. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  7. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  8. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  9. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

随机推荐

  1. HDU 4618 Palindrome Sub-Array(DP)

    题目链接 我还是图样啊....比赛的时候没敢暴力去搜... #include <cstdio> #include <cstdlib> #include <cstring& ...

  2. linux rootfs制作

    http://blog.sina.com.cn/s/blog_6795385f01011ifg.html 作一个嵌入式Linux rootfs,并且实现 web 服务 1. 文件系统简介 •理论上说一 ...

  3. python 操作execl文件

    http://www.jb51.net/article/60510.htm import xlrdimport xlwt # 打开文件   workbook = xlrd.open_workbook( ...

  4. Add Customerlize Button in More Button List In Odoo

    There're two commen type of actions in odoo: ir.actions.server,ir.actions.client_multi 1.Using ir.ac ...

  5. ThinkPHP之APP_DEBUG给我带来的问题

    1.刚开始学习Thinkphp,在模块分组之后,自己配置了模块分组后,发现打不开网页了,分组配置如图 2.问题现象如图 在处理空模块时写的函数也不能运行 这时我很困惑,一直检查自己的拼写和配置没发现错 ...

  6. pajax

    pjax网址:https://libraries.io/bower/yii2-pjax 1. 连接指定的div,实行pjax ,利用 linkSelector 方法<div id="c ...

  7. 如何搭建redis扩展-Yii中文网

    原文链接: 如何搭建redis扩展http://www.yii-china.com/post/detail/43.html 安装redis扩展: 1.通过composer进行安装,到项目根目录cmd运 ...

  8. javascript双击事件取消默认的两次单击事件

    当一个元素同时具有单击和双击事件时,双击时会触发2次单击和1此双击事件. 双击会:先第1次单击 ,同时触发第2次和双击事件. 造成的不好影响:每次单击事件会延迟执行. <!DOCTYPE htm ...

  9. freeswitch

    FreeSWITCH 是一个电话的软交换解决方案,包括一个软电话和软交换机用以提供语音和聊天的产品驱动.FreeSWITCH 可以用作交换机引擎.PBX.多媒体网关以及多媒体服务器等.可以用作一个简单 ...

  10. FZU 2171 线段树 区间更新求和

    很模板的题 在建树的时候输入 求和后更新 #include<stdio.h> #include<string.h> #include<algorithm> #inc ...