leetcode-mid-sorting and searching - 33. Search in Rotated Sorted Array
mycode
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if target in nums:
return nums.index(target)
else:
return -
参考:
二分查找
可以和34 Search for a Range对比思考,都是采用二分法哦!
class Solution:
# @param {integer[]} numss
# @param {integer} target
# @return {integer}
def search(self,nums,target):
if not nums:
return -
low,high=,len(nums)-
while low<=high:
mid=(low+high)/
if target==nums[mid]:
return mid
if nums[low]<=nums[mid]:
if nums[low]<=target<=nums[mid]:
high=mid-
else:
low=mid+
else:
if nums[mid]<=target<=nums[high]:
low=mid+
else:
high=mid-
return -
leetcode-mid-sorting and searching - 33. Search in Rotated Sorted Array的更多相关文章
- 【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 migh ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- LeetCode题解33.Search in Rotated Sorted Array
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- [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. ...
随机推荐
- .net core 2.2.0 SOAP踩坑
首先确认下面几个程序集是最新版本: <PackageReference Include="System.ServiceModel.Http" Version="4. ...
- JAVA重写不需要@override
一,如下代码, package com.boot.enable.bootenable; import org.springframework.scheduling.annotation.Async; ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- python图形图像处理--验证码的制作
from PIL import Image,ImageDraw,ImageFontimport randomfrom io import BytesIO class code(): def __ini ...
- Centos7安装Python3的方法[转]
Centos7安装Python3的方法 由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_ ...
- java面试题全集(上)
1.面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:抽象是将一类对象的共同特征总结出来构造类的过程,包括数据抽象和行为抽象两方面.抽象只关注对象有哪些属性和行为,并不关注 ...
- ifconfig命令返回找不到“-bash: ifconfig: command not found”
“-bash: ifconfig: command not found“因为系统没有安装net-tools yum -y install net-tools
- linux高性能服务器编程pdf免费下载
百度云盘:链接: https://pan.baidu.com/s/1pLp4hHx 密码: wn4k
- Spring的新注解
1 @Configuration 1.1 作用 用于指定当前类是一个Spring的配置类,当创建容器的时候会从该类上加载注解.获取容器的时候需要使用AnnotationApplicationConte ...
- 【NOIP2012模拟11.1】塔(加强)
题目 玩完骰子游戏之后,你已经不满足于骰子游戏了,你要玩更高级的游戏. 今天你瞄准了下述的好玩的游戏: 首先是主角:塔.你有N座塔一列排开.每座塔各自有高度,有可能相等. 这个游戏就不需要地图了. 你 ...