【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

标签: LeetCode


题目地址:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/

题目描述:

Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed? Would this affect the run-time complexity? How and why?

Suppose an array sorted in ascending order 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).

Find the minimum element.

The array may contain duplicates.

题目大意

找出可能含有重复数字的旋转有序数组中的最小值。

解题方法

这个题就是剑指offer中的原题。可以看我之前的博客http://blog.csdn.net/fuxuemingzhu/article/details/79501202

思想就是如果出现了重复数字,那么二分查找就没有作用了,必须使用顺序查找了。

class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
p1, p2 = 0, len(nums) - 1
mid = p1
while nums[p1] >= nums[p2]:
if p2 - p1 == 1:
mid = p2
break
mid = (p1 + p2) / 2
if nums[mid] == nums[p1] and nums[mid] == nums[p2]:
return self.minInOrder(nums, p1, p2)
if nums[mid] >= nums[p1]:
p1 = mid
elif nums[mid] <= nums[p2]:
p2 = mid
return nums[mid] def minInOrder(self, nums, index1, index2):
n1 = nums[index1]
for i in range(index1 + 1, index2):
if n1 > nums[i]:
return nums[i]
return n1

日期

2018 年 3 月 13 日

【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)的更多相关文章

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

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

  2. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

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

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

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

  5. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

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

  6. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  7. LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)

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

  8. LeetCode 154.Find Minimum in Rotated Sorted Array II(H)(P)

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

  9. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

随机推荐

  1. Linux-设置终端界面的字体颜色和自定义常用快捷功能

    .bashrc是一个隐藏的文件,要打开并修改该文件需要: (0)命令:cd ~ (1)命令:ls -a 找到文件 .bashrc: (2) 命令 vim ~/.bashrc 进入到文件: (3) 直接 ...

  2. Spring Cloud 2021.0.0 正式发布,第一个支持Spring Boot 2.6的版本!

    美国时间12月2日,Spring Cloud 正式发布了第一个支持 Spring Boot 2.6 的版本,版本号为:2021.0.0,codename 为 Jubilee. 在了解具体更新内容之前, ...

  3. A Child's History of England.48

    A few could not resolve to do this, but the greater part complied. They made a blazing heap of all t ...

  4. Docker学习(六)——Dockerfile文件详解

    Docker学习(六)--Dockerfile文件详解 一.环境介绍 1.Dockerfile中所用的所有文件一定要和Dockerfile文件在同一级父目录下,可以为Dockerfile父目录的子目录 ...

  5. 4.3 rust func closure

    fn add_one_v1 (x: u32) -> u32 { x + 1 } let add_one_v2 = |x: u32| -> u32 { x + 1 }; let add_on ...

  6. 数据库ER图基础概念

    ER图分为实体.属性.关系三个核心部分.实体是长方形体现,而属性则是椭圆形,关系为菱形. ER图的实体(entity)即数据模型中的数据对象,例如人.学生.音乐都可以作为一个数据对象,用长方体来表示, ...

  7. 【Linux】【Services】【SaaS】Docker+kubernetes(7. 安装Docker私有镜像仓库)

    1. 简介 1.1. 自己做个私有镜像,方便上传和下载,我也在docker官网注册了一个账号,做好的镜像可以传上去 1.2. Redhat自带私有镜像的功能,需要安装包,这是howto: https: ...

  8. 【Linux】【Services】【Package】rpm

    CentOS系统上rpm命令管理程序包:         安装.升级.卸载.查询和校验.数据库维护                   rpm命令:rpm  [OPTIONS]  [PACKAGE_F ...

  9. eclipse.ini顺序

    -vmargs需放在-Dfile.encoding=UTF-8之前,否则会出现乱码 举例: -startup plugins/org.eclipse.equinox.launcher_1.3.0.v2 ...

  10. Springboot集成SpringSecurity

    一.Spring security 是什么? Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架. 它提供了一组可以在Spring应用上 ...