题目描述:

python实现 Search in Rotated Sorted Array2 搜索旋转排序数组  

  中文:

  假设按照升序排序的数组在预先未知的某个点上进行了旋转。

  ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] )。

  编写一个函数来判断给定的目标值是否存在于数组中。若存在返回 true,否则返回 false。

  英文:

  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.

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

题目来源:力扣题库

LeetCode(力扣)——Search in Rotated Sorted Array2 搜索旋转排序数组 python实现的更多相关文章

  1. LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

    题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组   中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...

  2. leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7  ...

  3. 033 Search in Rotated Sorted Array 搜索旋转排序数组

    假设按照升序排序的数组在预先未知的某个关键点上旋转.(即 0 1 2 4 5 6 7 将变成 4 5 6 7 0 1 2).给你一个目标值来搜索,如果数组中存在这个数则返回它的索引,否则返回 -1.你 ...

  4. Leetcode33.Search in Rotated Sorted Array搜索旋转排序数组

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个目标值, ...

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

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. [Leetcode] search in rotated sorted array 搜索旋转有序数组

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7might ...

  7. LeetCode 153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中的最小值 (C++)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  8. [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

随机推荐

  1. QTimer不能同时使用两个,用QTimerEvent (QT)

    最近写程序的时候有个界面想定两个QTimer定时器,不同时间干不同的事: QTimer *timer1 = new QTimer(this); QTimer *timer2 = new QTimer( ...

  2. C# 与 C++,语法差别有多小-第三章 C++数据类型 第一部分

    一,数据类型 C++: char  int short  long float double, unsigned long double(128位,19位有效数字), wchar_t, 浮点型文字常量 ...

  3. 【Nacos】Nacos安装

    1.Nacos简介 Nacos是阿里巴巴集团开源的一个易于使用的平台,专为动态服务发现,配置和服务管理而设计.它可以帮助您轻松构建云本机应用程序和微服务平台. Nacos基本上支持现在所有类型的服务, ...

  4. linux vim设置和 快捷命令配置

    1.vim配置 set tabstop= set shiftwidth= set softtabstop= set fileencodings=utf-,ucs-bom,gb2312,gbk,gb18 ...

  5. create-react-app下的@修饰器正确的使用方式记录

    在create-react-app下使用es7的@修饰器会报错''Support for the experimental syntax 'decorators-legacy' isn't curre ...

  6. unittest测试框架生成可视化测试报告-BeautifulReport

    生成报告的样式: 在说unittest之前,先说几个概念: TestCase 也就是测试用例 TestSuite 多个测试用例集合在一起,就是TestSuite TestLoader是用来加载Test ...

  7. SVN 忽略添加文件和文件夹

    你添加的文件和文件夹是没有加入版本控制的,是你新添加的,接下来的设置才有用 忽略这个文件的方式有两种 第一种方式 添加svn:ignore    右键文件-->TortoiseSvn--> ...

  8. Vagrant 手册之 box - 创建基础 box

    原文地址 有一种特殊的 box 被称为"base box".这些 box 包含 Vagrant 运作所需的最低限度,通常不是对现有的 Vagrant 环境("base b ...

  9. PHP批量生成底部带编号二维码(二维码生成+文字生成图片+图片拼接合并)

    PHP批量生成带底部编号二维码(二维码生成+文字生成图片+图片拼接合并) 需求: 输入编号如 : cb05-0000001  至 cb05-0000500 批量生成 以编号为名称的下图二维码,然后压缩 ...

  10. MySQL 查询语句--------------进阶5:分组查询

    #进阶5:分组查询 /* select 分组函数,列(要求出现在group by的后面) from 表 [where 筛选条件] group by 分组的列表 [order by 子句] 注意: 查询 ...