【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. VMware和Centos的安装及配置

    目录 1. 安装VMware 2. 安装CentOS6及配置 2.1 Centos安装 2.1.1 配置网络连接的三种形式 2.1.1.1 桥连接 2.1.1.2 NAT模式 2.1.1.3 主机模式 ...

  2. Linux三剑客之老三grep

    说明: Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.工作中我们常常用它来过滤出我们想要的数据. 格式: grep [OPTIONS] 基本参 ...

  3. C/C++运行时确定字节顺序

    字节顺序(英文:Endianness),多字节数据在内存中的存储顺序: 1.对于特定数据,内存空间有起始地址.结束地址: 2.对于数据本身,存在高位字节.地位字节:例如 int data = 0x01 ...

  4. euerka总结

    一.euerka的基本知识 1. 服务治理 Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务治理 在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系 ...

  5. Hive(五)【DQL数据查询】

    目录 一. 基本查询 1.1 算数运算符 1.2 常用聚合函数 1.3 limit 1.4 where 1.5 比较运算符(between|in|is null) 1.6 LIKE和RLIKE 1.7 ...

  6. jquery:iframe里面的元素怎样触发父窗口元素的事件?

    例如父窗口定义了一个事件. top: $(dom1).bind('topEvent', function(){}); 那么iframe里面的元素怎样触发父窗口dom1的事件呢?这样吗? $(dom1, ...

  7. Tomcat简单介绍

    1.目录结构 在conf文件夹中修改了配置之后一定要重启Tomcat

  8. 计算机网络 Raw_Socket编程 Ping C语言

    计算机网络做了一个附加题,用C语言Raw_Socket实现ping指令. 通过本部的Mooc学习了一下Socket编程,然后成功写了出来orz 先放一下代码: #include <stdio.h ...

  9. Linux实体服务器添加网卡

    目录 一.简介 二.配置 三.添加网卡 四.总结 一.简介 服务器如果搭配了网口,在插入网线或者光纤后会亮灯.如果发现不亮,可以关闭机器查看亮不亮,因为有的时候系统会把网口禁用,进入到系统反而不亮了, ...

  10. Hibernate框架使用之环境搭建

    第一步:引入所需的jar包 第二步:创建实体类,配置实体类与数据表的映射关系 创建实体类 User.java package cn.hao.entity; public class User { /* ...