[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 ...
随机推荐
- BZOJ3103 : Palindromic Equivalence
用Manacher可以推出O(n)对相等和不等关系. 将相等的用并查集维护,不等的连边. 然后从1到n,如果该等价类还没被考虑过,则ans*=26-与它不等的考虑过的等价类个数. #include&l ...
- 数据仓库原理<1>:数据库与数据仓库
updated 2015.8.27 updated 2015.8.26 updated 2015.8.23 0. 说明 <数据仓库原理>系列博文,是笔者在学习数据仓库与商业智能时的读书笔记 ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- NOI2011阿狸的打字机(fail树+DFS序)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- IntelliJ IDEA 14 SVN无法正常使用问题
通过SVN导入项目 SVN checkout时候会出现如下错误: Cannot run program "svn" (in directory "E:\Projects& ...
- QDir, QFileInfo 和 QDirIterator 区别
这三个类相互有关联,但是有不尽相同,首先从名字上看,QDir 和 QDirIterator 是针对于文件目录的,也就是文件夹,我们知道,对于一个文件夹,可以包含很多文件,也可以包含其他文件夹,通常是一 ...
- people 0919
package liu0919; public class People { private double height;// 身高 private String name;// 名字 private ...
- hdu 4310 Hero
这道题是道算是一道很简单的贪心题了,但是要注意排序的依据,这道题是按照dps/hp的从大到小排序的,然后计算总的sumhp即可. #include"iostream" #inclu ...
- .net 后台设置meta的属性(keywords,description)
首先在前台aspx文件中的head标记添加runat="server"的属性. 之后后台如下编写: protected void Page_Load(object sender, ...
- 3. PHP
安装: apt-get install php5-fpm php5-mysql 配置: vi /etc/php5/fpm/php.ini cgi.fix_pathinfo=0 vi /etc/ ...