[leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.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.
解题思路:还是二分查找的变种。需要考虑好边界情况。
代码:
class Solution:
# @param A a list of integers
# @param target an integer
# @return a boolean
def search(self, A, target):
left=0; right=len(A)-1
while left<=right:
mid=(left+right)/2
if A[mid]==target: return True
if A[left]==A[mid]==A[right]:
left+=1; right-=1
elif A[left]<=A[mid]:
if A[left]<=target<A[mid]: right=mid-1
else: left=mid+1
else:
if A[mid]<=target<A[left]: left=mid+1
else:right=mid-1
return False
[leetcode]Search in Rotated Sorted Array II @ Python的更多相关文章
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [Leetcode] search in rotated sorted array ii 搜索旋转有序数组
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Search in Rotated Sorted Array II 二分搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 配置lambda
=========== 添加 apply plugin: 'me.tatarka.retrolambda' 添加 compileOptions { sourceCompatibilit ...
- JZYZOJ 2041 快速数论变换 NTT 多项式
http://172.20.6.3/Problem_Show.asp?id=2041 https://blog.csdn.net/ggn_2015/article/details/68922404 代 ...
- BZOJ3779 : 重组病毒
一个点的感染时间为它到根路径上虚边数+1. 用Link-Cut Tree模拟虚实边切换,每次切换时等价于在一段或两段DFS序区间更新,线段树维护即可. 时间复杂度$O(n\log^2n)$. #inc ...
- 使用Newlife网络库管道模式解决数据粘包(二)
上一篇我们讲了 如何创建一个基本的Newlife网络服务端 这边我们来讲一下如何解决粘包的问题 在上一篇总我们注册了Newlife的管道处理器 ,我们来看看他是如何实现粘包处理的 svr.Add< ...
- Regex.Split
private static List<int> GetThemeIds(string themeList) { const string split = "!===!" ...
- 在Eclipse添加Android兼容包( v4、v7 appcompat )(转)
昨天添加Android兼容包,碰到了很多问题,在这里记录一下,让后面的路好走. 如何选择兼容包, 请参考Android Support Library Features(二) 一.下载Support ...
- Linux下的sqlserver简单试用
微软自2017年就推出了可以在linux上使用的sql-server,最近接触到了一个用sqlserver的项目,便尝试使用了一下. 下载 为了简化安装,我还是使用的docker的方式,镜像可以直接从 ...
- .net core下的dotnet全局工具
.net core 2.1后支持了一个全新的部署和扩展命令,可以自己注册全局命令行. dotnet install tool -g dotnetsaydotnetsay 也可以自己构建自己的命令行,一 ...
- Python 訪问 LinkedIn (API)
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-16 @author: guaguastd @name: l ...
- Dapper-translation 分布式监控系统
http://bigbully.github.io/Dapper-translation/ https://github.com/bigbully/Dapper-translation