[LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源
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的更多相关文章
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 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 ...
- 081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||
这是 “搜索旋转排序数组”问题的跟进:如果数组元素允许重复,怎么办?这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?假设按照升序排序的数组在预先未知的某个关键点上旋转.(例如, 0 1 2 4 ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 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 ...
随机推荐
- 转盘游戏[XDU1006]
Problem 1006 - 转盘游戏 Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 87 Accep ...
- python 代码片段15
#coding=utf-8 ''' 如果运行时发生异常的话,解释器会查找相应的处理函数.要是在当前函数里没有 找到的话,它会将异常传递给上层的调用函数,看看那里能不能处理.如果在在最 外层还没有找到的 ...
- Quartz Cron 表达式
Cron 表达式包括以下 7 个字段 格式: [秒] [分] [小时] [日] [月] [周] [年] 说明 是否必填 允许填写的值 允许的通配符 秒 是 - , - * / 分 是 - , - * ...
- TYVJ P1075 硬币游戏 Label:dp
背景 农民John的牛喜欢玩硬币,所以John就为它们发明了一个新的两人硬币游戏,叫做Xoinc. 描述 最初地面上有一堆n个硬币(5<=n<=2000),从上面数第i个硬币的价值为C_i ...
- [Cocos2d-x For WP8]Menu菜单
菜单栏这是游戏里面基本的控件来的, 头文件: #include <CCMenu.h> 继承关系图: 头文件: #include < ...
- SpringMVC_The resource identified by this request is only capable of generating responses with characteristics
今天在调试springMVC的时候,在将一个对象返回为json串的时候,浏览器中出现异常: The resource identified by this request is only capabl ...
- SSh结合Easyui实现Datagrid的分页显示
近日学习Easyui,发现非常好用,界面很美观.将学习的心得在此写下,这篇博客写SSh结合Easyui实现Datagrid的分页显示,其他的例如添加.修改.删除.批量删除等功能将在后面的博客一一写来. ...
- [ZT] Vim快捷键分类
一. 移动: h,j,k,l: 左,下,上,右. w: 下一个词的词首. e:下一个词的词尾. b:上一个词的词首. <>: v 模式选中后进行缩进.二. 跳 ...
- PB 简单笔记!
1.总体说明: a) 程序不区分大小写 b) 赋值用= String city=“南京”,country ;Integer person[3]={3,8,9};String s = ' You ...
- 【iCore3双核心板】扩展引脚分布
PDF 版下载: http://files.cnblogs.com/files/xiaomagee/iCore3%E6%89%A9%E5%B1%95%E5%BC%95%E8%84%9A%E5%88%8 ...