题目来源


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. android 获取当前屏幕作为毛玻璃模糊背景Acitivity作为弹出框。

    使用: 1.在执行弹出界面前,先将其当前屏幕截图. BlurBuilder.snapShotWithoutStatusBar(getActivity()); 2.为了确保界面切入无效果. startA ...

  2. Codeforces Round #203 (Div. 2) A.TL

    #include <iostream> #include <algorithm> using namespace std; int main(){ int n,m; cin & ...

  3. ios ASIHttpLib 同步请求和异步请求

    1.同步请求可以从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作, 2.异步请求不会阻塞主线程,而会建立一个新的线程来操作,用户发出异步请求后,依然 ...

  4. 【BZOJ】1098: [POI2007]办公楼biu(补图+bfs+链表)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1098 显然答案是补图连通块..... 想到用并查集...可是连补图的边都已经...n^2了...怎么 ...

  5. Genymotion填坑之路

    Genymotion是一款android的模拟器,之前用的一台电脑直接装上就可以用,后来换了一台机器,一直报获取不到IP,后来网上各种找方法,偶然发现网上说的是硬件问题: 在BIOS中将CPU的vir ...

  6. [LintCode] Implement Trie 实现字典树

    Implement a trie with insert, search, and startsWith methods. Have you met this question in a real i ...

  7. [zt]不到最后一秒你永远不知道结局且震撼你心灵的高端电影

    总有一部电影,让你憋着尿直到看完~~~ http://share.renren.com/share/230538513/17679574169?from=0101090202&shfrom=0 ...

  8. cookie 用户第一次访问时执行

    <script type="text/javascript" src="/js/jquery.Cookies.js"></script> ...

  9. order by id asc得出的排序是什么原理

    我们要用order by id asc得出的排序应该是,4,好了原理就这么简. sql实现方法,代码如下: : 代码如下: $sql ="Select 字段 from 表名 where id ...

  10. Linux权限问题

    Linux中各个用户对文件和目录的权限有三种: r: 读 w:写 x:执行 各个权限的意义: 文件 r:可以读取文件的内容 w:编辑文件内容 x:执行该文件 目录 r:列出该目录下的内容,即使用ls命 ...