leetcode 【 Search in Rotated Sorted Array II 】python 实现
题目:
与上一道题几乎相同;不同之处在于array中允许有重复元素;但题目要求也简单了,只要返回true or false
http://www.cnblogs.com/xbf9xbf/p/4254590.html
代码:oj测试通过 Runtime: 73 ms
class Solution:
# @param A a list of integers
# @param target an integer
# @return a boolean
def search(self, A, target):
A=list(set(A))
# none case & zero case
if A is None or len(A)==0 :
return False
# binary search
start = 0
end = len(A)-1
while start<=end :
# one element left case
if start == end :
if A[start]==target :
return True
else:
return False
# two elements left case
if start+1 == end :
if A[start]==target :
return True
elif A[end]==target :
return True
else:
return False
# equal or more than three elements case
mid = (start+end)/2
if A[mid]==target :
return True
elif A[mid]>target:
if A[start]>A[mid] and A[end]<A[mid]:
start = mid+1
elif A[start]<A[mid] and A[end]<A[mid]:
if A[end]>=target:
start = mid+1
else:
end = mid-1
elif A[start]>A[mid] and A[end]>A[mid]:
end = mid-1
else:
end = mid-1
else:
if A[start]>A[mid] and A[end]<A[mid]:
end = mid-1
elif A[start]<A[mid] and A[end]<A[mid]:
start = mid+1
elif A[start]>A[mid] and A[end]>A[mid]:
if A[end]>=target :
start = mid+1
else:
end = mid-1
else:
start = mid+1
return False
思路:
用了一个trick Python数组去重的办法A=list(set(A))
这样A数组中就没有重复的元素了,可以直接用之前一题的代码。
这样的trick应该不是题目的本意,这道二分查找题目比较经典,应该吃透。
leetcode 【 Search in Rotated Sorted Array II 】python 实现的更多相关文章
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- 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 ...
随机推荐
- MyBatis源码探索
每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为中心的.SqlSessionFactory 的实例可以通过 SqlSessionFactoryBuilder 获 ...
- 基于jeesit下的工作流开发步骤
首先jeesit是开源的OA系统,采用的框架是springMVC和mybatis,采用shiro安全验证. 1.新建流程所属表: 在数据库新建所需工作流的表之后,登录jeesit系统,在“代码生成”- ...
- @Valid的坑
@Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前
- mybatis-动态sql1
在多条件查询的情况下必须用到动态sql 沿用之前的项目 1.在dao中添加多添件查询方法 package com.java1234.mappers; import java.util.List;imp ...
- PHP与MYSQL结合操作——文章发布系统小项目(实现基本增删查改操作)
php和mysql在一起几十年了,也是一对老夫老妻了,最近正在对他们的爱情故事进行探讨,并做了一个很简单的小东西——文章发布系统,目的是为了实现mysql对文章的基本增删查改操作 前台展示系统有:文章 ...
- windows添加快速启动栏
步骤: 右击任务栏——选择“新建工具栏” 在“文件夹”路径中填入%appdata%\Microsoft\Internet Explorer\Quick Launch并单点“选择文件夹” 右键单击任务栏 ...
- linux 命令——37 date (转)
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- POJ 2385 Apple Catching(01背包)
01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...
- SPOJ - MATSUM Matrix Summation---二维树状数组
题目链接: https://vjudge.net/problem/SPOJ-MATSUM 题目大意: 二维数组,两种操作 SET 将某点设置成x SUM 求某个区域之和 解题思路: 这里用二维树状数组 ...
- django+xadmin在线教育平台(五)
3-3 django orm介绍与model设计 上节教程完成后代码(来学习本节前置条件): 对应commit: 留言板前端页面展示.本次内容截止教程3-2结束. 可能现在你还在通过手写sql语句来操 ...