题目来源


https://leetcode.com/problems/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 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.


题意分析


Input: a list with rotated sorted array with a unknown pivot

Output:index or -1

Conditions:在一个翻转数组中,找到target的位置,若不能找到则返回-1


题目思路


此题是关于二分查找的题目, 关键在于确定二分的位置,注意到数组仅是关于一个pivot位置的翻转,意味着这个pivot之前的值都是大于pivot之后的值的,所以在二分的时候利用好这个信息就好了

  1. 用first和last存储首尾位置,mid = (first + mid)// 2;
  2. 如果target等于nums[mid],返回target值;
  3. 如果nums[first] < nums[mid]:
  4. 如果nums[first] >= nums[mid]
  5. 对3,4两种条件,加上target与nums[mid]的值的比较,与target与nums[first]的比较,则可确定新的first与last的范围

AC代码(Python)


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

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

  1. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  2. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  3. LeetCode 033 Search in Rotated Sorted Array

    题目要求:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you b ...

  4. Java for LeetCode 033 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 ...

  5. leetcode第32题--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 7  ...

  7. LeetCode(33)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 m ...

  8. 【LeetCode】033. Search in Rotated Sorted Array

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

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

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

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

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

随机推荐

  1. BZOJ2102 : [Usaco2010 Dec]The Trough Game

    暴力枚举答案然后检验. #include<cstdio> int n,m,i,j,k,a[100],b[100],cnt,ans;char s[20]; int main(){ for(s ...

  2. POJ 3352 (边双连通分量)

    题目链接: http://poj.org/problem?id=3352 题目大意:一个连通图中,至少添加多少条边,使得删除任意一条边之后,图还是连通的. 解题思路: 首先来看下边双连通分量的定义: ...

  3. 面试题中遇到的SQL题目

    1.假设有一张表示cj表 Name Subject Result 张三 语文 80 张三 数学 90 张三 物理 85 李四 语文 85 李四 数学 92 李四 物理 82 要求查询结果: 姓名 语文 ...

  4. php读取3389脚本

    <?php $regkey = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Td ...

  5. 禁用符合一定条件的AD对象 (含Filter参数的写法)

    Get-ADComputer -Filter "(sAMAccountType -eq 805306369)" -SearchBase "OU=Computers,OU= ...

  6. Yii2 Format 如何使用

    $formatter = \Yii::$app->formatter; // output: January 1, 2014 echo $formatter->asDate('2014-0 ...

  7. GDC 2016 神秘海域4中使用Substance制作Texture

    TEXTURING UNCHARTED 4: A MATTER OF SUBSTANCE 原文链接 http://www.dualshockers.com/2016/03/16/amazing-unc ...

  8. ThinkPHP验证码刷新随机数

    貌似因为IE的内核不支持重复,,所以要加个随机数..在代码中,,发现火狐的也不行..加了随机数后就可以了 <label class="img"><img id=& ...

  9. 字符串复制char *strcpy(char* dest, const char *src);

    ⒈strcpy的实现代码 char * strcpy(char * strDest,const char * strSrc) { if ((NULL==strDest) || (NULL==strSr ...

  10. PHP文件操作 之打开远程文件

    //配置php.ini 开启allow_url_fopen选项 //访问的文件有可读或者可写的权限 //$f = fopen('http://www.example.com/a.txt','rb'); ...