LeetCode上牵扯到Rotated Sorted Array问题一共有四题,主要是求旋转数组的固定值或者最小值,都是考察二分查找的相关知识。在做二分查找有关的题目时,需要特别注重边界条件和跳出条件。在做题的过程中,不妨多设几个测试案例,自己判断一下。

下面是这四题的具体解答。

33.Search in Rotated Sorted Array

在求旋转数组中查找固定值,数组中每个数唯一出现。返回查找索引。

class Solution:
def search(self, nums: List[int], target: int) -> int:
lo,hi = 0,len(nums)-1
while lo<=hi:
mid = lo+((hi-lo)>>1)
if nums[mid] == target:
return mid
if nums[lo] <= nums[mid]:
if nums[lo] <= target <= nums[mid]:
hi = mid -1
else:
lo = mid + 1
else:
if nums[mid] <= target <=nums[hi]:
lo = mid + 1
else:
hi = mid - 1
return -1

81.Search in Rotated Sorted Array II

在求旋转数组中查找固定值,数组中可能出现重复值。返回True或者False

class Solution(object):
def search(self, nums, target):
if not nums:
return False
low, high = 0, len(nums) - 1
while low <= high:
mid = low+(high-low) // 2
if target == nums[mid]:
return True
if nums[low] < nums[mid]:
if nums[low] <= target <= nums[mid]:
high = mid - 1
else:
low = mid + 1
elif nums[mid]<nums[low]:
if nums[mid] <= target <= nums[high]:
low = mid + 1
else:
high = mid - 1
else:
low+=1
return False

153.Find Minimum in Rotated Sorted Array

在求旋转数组中查找最小值,数组中每个数唯一出现。返回最小值。

class Solution:
def findMin(self, nums: List[int]) -> int:
lo,hi = 0,len(nums)-1
if nums[lo] < nums[hi]:return nums[lo] # 递增
while hi-lo > 1:
mid = lo+(hi-lo)//2
if nums[lo] > nums[mid]:
hi = mid
elif nums[hi] < nums[mid]:
lo = mid
return nums[hi]

154.Find Minimum in Rotated Sorted Array II

在求旋转数组中查找最小值,数组中可能出现重复值。返回最小值。

class Solution:
def findMin(self, nums: List[int]) -> int:
if not nums:return
lo,hi = 0,len(nums)-1
if nums[lo] < nums[hi]:return nums[lo] # 递增
minVal = nums[lo]
while hi-lo>1:
mid = lo+(hi-lo)//2
if nums[lo] > nums[mid]:
hi = mid
elif nums[hi] < nums[mid]:
lo = mid
elif nums[mid] == nums[lo] == nums[hi]:
for i in range(lo,hi):
if nums[i] < minVal:
minVal = nums[i]
hi = i
return minVal
return nums[hi]

[Leetcode]Rotated Sorted Array问题的更多相关文章

  1. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  2. [LeetCode] Find Minimum 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 ...

  3. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

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

  5. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  6. Find Minimum in Rotated Sorted Array leetcode

    原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...

  7. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  8. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  9. Java for LeetCode 153 Find Minimum 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 ...

随机推荐

  1. 【数据库开发】 C连接mysql——常用的函数

    转载:http://blog.chinaunix.net/uid-26758020-id-3288633.html 1.MYSQL *mysql_init(MYSQL *mysql)    为mysq ...

  2. Android 反编译技术流程

    为何需要反编译 作为一名Android开发者,很多的时候需要去学习别人优秀的代码,原本在GitHub上就有很多开源的项目代码,但有的时候在使用软件时候遇到自己想要的功能时,想要学习实现的代码时,这时候 ...

  3. NLog文章系列—系列文章目录以及简要介绍

    参考文章:http://www.cnblogs.com/dflying/archive/2006/12/04/581750.aspx

  4. Rhino脚本引擎技术介绍

    引用:http://p.primeton.com/articles/54c1e255be20aa4735000001 http://blog.csdn.net/u013292493/article/d ...

  5. selenium + python 环境配置 (一)

    超级无敌菜鸟 终于有空学习一下python 和 selenium 啦 第一步: 环境配置  (Windows版) 1. 下载安装python 根据你的电脑,下载一个python吧   这儿装的是pyt ...

  6. Linux系统目录的学习

    1.在公司中linux 都是没有界面 2.系统路径    2.1 /表示根目录    2.2 ~表示/root    2.3etc:存放系统配置文件    2.4 home  除了root 以外所有用 ...

  7. Django与JS交互的示例代码-django js 获取 python 字典-Django 前后台的数据传递

    Django与JS交互的示例代码 Django 前后台的数据传递 https://www.cnblogs.com/xibuhaohao/p/10192052.html 应用一:有时候我们想把一个 li ...

  8. BeginLinux Programming chapter16: X11桌面系统简介

    当前两个最流行的linux desktop environment: GNOME 和KDE, 两者对应的图形库分别是 GTK+ 和 QT. GNOME与KDE与X11的关系: X defines no ...

  9. Android MTK6580 客制化关机充电动画

    1.客制化关机充电图片 vendor/mediatek/proprietary/bootable/bootloader/lk/dev/logo/xxx 找到对应分辨率替换 2.调整显示图片位置.大小 ...

  10. java学习基础知识入门

    基础入门知识(一) 一.java技术的分类 java按照技术标准和应用场景的不同分为三类,分别是JAVASE.JAVAEE.JAVAME JAVASE : 平台标准版,用于开发部署桌面,服务器以及嵌入 ...