[LeetCode]题解(python):033-Search in Rotated Sorted Array
题目来源
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之后的值的,所以在二分的时候利用好这个信息就好了
- 用first和last存储首尾位置,mid = (first + mid)// 2;
- 如果target等于nums[mid],返回target值;
- 如果nums[first] < nums[mid]:
- 如果nums[first] >= nums[mid]
- 对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的更多相关文章
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 【LeetCode】033. Search in Rotated Sorted Array
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 033 Search in Rotated Sorted Array 搜索旋转排序数组
假设按照升序排序的数组在预先未知的某个关键点上旋转.(即 0 1 2 4 5 6 7 将变成 4 5 6 7 0 1 2).给你一个目标值来搜索,如果数组中存在这个数则返回它的索引,否则返回 -1.你 ...
- LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现
题目描述: python实现 Search in Rotated Sorted Array 搜索旋转排序数组 中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1 ...
随机推荐
- Java虚拟机工作原理详解
原文地址:http://blog.csdn.net/bingduanlbd/article/details/8363734 一.类加载器 首先来看一下java程序的执行过程. 从这个框图很容易大体上了 ...
- .NET中的视图和过滤器 (DefaultView和RowFilter)
NET中的视图和过滤器 (DefaultView和RowFilter) ADO.NET中有一层对象,用来创建任意数据源的抽象模型.其中包括DataSet,DataTable,DataRow,DataV ...
- Lambda表达式可以被转换为委托类型
void Main() { //向Users类中增加两人; List<Users> user=new List<Users>{ new Users{ID=1,Name=&quo ...
- 最近公共祖先(lca)
囧啊囧. lca的求法太多了 倍增,tarjan,st,lct,hld.... 后边三个我就不写了,其中st我没写过,估计用不上,在线用倍增,离线用tarjan就行了. 嗯. 第一种,倍增(O(nlo ...
- POJ 2112 Optimal Milking(最大流+二分)
题目链接 测试dinic模版,不知道这个模版到底对不对,那个题用这份dinic就是过不了.加上优化就WA,不加优化TLE. #include <cstdio> #include <s ...
- android ScrollView滚动距离和判断滚动停止状态
今天很高兴,自己解决了判断ScrollView滚动停止的监听,现在分享给大家. 因为ScrollView没有像listView中的setOnScrollListener()监听,当然也就没有SCROL ...
- Qt5.4 VS2010 Additional Dependancies
Go to Linker -> General -> Additional LIbrary Directories: qtmaind.libQt5Cored.libQt5Guid.libQ ...
- Java主要有那几种文件类型,各自作用
java提供了两类数据类型:基本类型,引用类型. 1.基本类型包括:boolean ,byte,char,int,short,float,long,double,值得注意的是:在原始数据类型中,除了b ...
- sql对应C#的类型
- 【iM_VGA模块】运行 ucgui 演示!
挂在 iCore2 双核心板上的 VGA模块,跑 ucgui DEMO 演示.大家看看! ============================== 技术论坛:http://www.eeschool ...