• 题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值;

  • 思路:

  1. 第一遍二分遍历,找到数组中最小值的索引;
  2. 第二遍分别对最小值左右两边的数组进行二分查找;
class Solution(object):

    def find_min(self, nums):
if not nums:
return -1
left, right = 0, len(nums) - 1
while nums[left] > nums[right]:
if right - left == 1:
return right
mid = (left + right) / 2
if nums[left] <= nums[mid]:
left = mid
if nums[right] >= nums[mid]:
right = mid
return 0 def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if not nums:
return -1
min_index = self.find_min(nums)
if nums[min_index] == target:
return min_index
elif nums[min_index] > target:
return -1
else:
left = self.search_t(nums, 0, min_index, target)
if left >= 0:
return left
right = self.search_t(nums, min_index, len(nums)-1, target)
if right >= 0:
return right
return -1 def search_t(self, nums, left, right, target):
while left <= right:
mid = (left + right) / 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

Python 解LeetCode:33. Search in Rotated Sorted Array的更多相关文章

  1. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  2. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  3. [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. ...

  4. 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. ...

  5. leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  6. LeetCode 33.Search in Rotated Sorted Array(M)

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

  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 migh ...

  8. Java [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 ...

  9. [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. ...

  10. LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)

    题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description   Problem :当前的数组 ...

随机推荐

  1. bbs-admin-自定义admin(二)

    本文内容 目的:模仿admin默认配置,自定义配置类 一 查 1 查看数据 2 查看表头 3 分页器 4 search(搜索框)   5 action(批量处理)    6 filter(分类)   ...

  2. Java面向对象2(G~J)

    G    织女的红线(SDUT 2240) import java.util.Scanner; import java.text.DecimalFormat; class Sum { double x ...

  3. 卸载Ambari集群

    清理ambari安装的hadoop集群 本文针对redhat或者centos 对于测试集群,如果通过ambari安装hadoop集群后,想重新再来一次的话,需要清理集群. 对于安装了很多hadoop组 ...

  4. PG数据库创建并执行存储过程批量插入数据

    记录一下PG数据库创建并执行存储过程批量插入数据的SQL: create or replace function addId() returns boolean AS $BODY$ declare i ...

  5. [GIT]比较不同分支的差异

        比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: undefined 1.查看 dev 有,而 master 中没有的: 1.查看 de ...

  6. 外部连接mysql docker容器异常

    为了方便,使用python测试连接mysql容器 脚本内容非常简单 #!/usr/bin/python3 import pymysql conn=pymysql.connect(host=,passw ...

  7. 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)

    在前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来: 工程下载:https://files.cnblogs.com/ ...

  8. C之交换数据案例

    //值传递 void swap(int i,int j){ printf("交换后:\n "); int tmp; tmp = i; i = j; j = tmp; } //引用传 ...

  9. object_detection faster-rcnn

    (t20190518) luo@luo-All-Series:~/MyFile/TensorflowProject/Faster_RCNN/models/research$ (t20190518) l ...

  10. php开启短标签与<?xml version="1.0" encoding="UTF-8"?>冲突

    前两天写了个生成mapsite的源码,想提交到百度和谷歌,在本地测试一点问题都没有,但是在服务器上就显示500错误 最后废了九牛二虎之力,终于可以设置iis直接显示出错信息. 也搞懂了问题所在,默认服 ...