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. 欧姆龙 EntherNet/IP(CIP报文格式)

    Enthip/IP_ CIP报文格式 测试Demo在文章末尾 注册请求帧: 0x65 0x00   注册请求命令 2byte 0x04,0x00   header长度2byte   < 封装头& ...

  2. 最新 拉卡拉java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.拉卡拉等10家互联网公司的校招Offer,因为某些自身原因最终选择了拉卡拉.6.7月主要是做系统复习.项目复盘.LeetCo ...

  3. Eclipse启动时报错Java was started but returned exit code=13

    Eclipse启动时报错Java was started but returned exit code=13 如图所示 原因是通过第三方更新JRE时,第三方安装的是32位的JRE,与64位的eclip ...

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

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

  5. vue-cli开发-搭建项目(一)

    前言 vue-cli是Vue官方提供的命令行工具,可用于快速搭建大型单页应用.集成了webpack环境及主要依赖,对于项目的搭建.打包.维护管理等都非常方便快捷.建议先熟悉 Vue 本身之后再研究 C ...

  6. Redis 常用命令学习二:字符串类型命令

    1.赋值与取值命令 127.0.0.1:6379> set foo helloredis OK 127.0.0.1:6379> get foo "helloredis" ...

  7. 20191011-构建我们公司自己的自动化接口测试框架-Util的读取excel常用方法模块

    包括获取excel的sheet名字,设定excel的sheet,读excel,写excel等常规操作. from openpyxl import Workbook from openpyxl impo ...

  8. 解析spring启动加载dubbo过程

    一:简单配置 web.xml <context-param> <param-name>contextConfigLocation</param-name> < ...

  9. Tokitsukaze and Strange Rectangle CodeForces - 1191F (树状数组,计数)

    大意: 给定$n$个平面点, 定义集合$S(l,r,a)$表示横坐标$[l,r]$纵坐标$[a,\infty]$内的所有点. 求可以得到多少种不同的集合. 从上往下枚举底层最右侧点, 树状数组统计贡献 ...

  10. 使用UTF8字符集存储中文生僻字

    使用UTF8字符集存储中文生僻字 一.相关学习BLOG https://www.cnblogs.com/jyzhao/p/8654412.html http://blog.itpub.net/7818 ...