【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
【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)的更多相关文章
- [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 ...
- 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 ...
- [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. ...
- 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 ...
- 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 ...
- [LeetCode#154]Find Minimum in Rotated Sorted Array II
The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...
- 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. ( ...
- 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. ( ...
- 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
随机推荐
- 转-nRF5 SDK for Mesh(六) BLE MESH 的 基础概念
nRF5 SDK for Mesh(六) BLE MESH 的 基础概念 Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile s ...
- acupuncture
acute+puncture. [woninstitute.edu稻糠亩] To understand the basics of acupuncture, it is best to familia ...
- pymongdb入门
Pymongo入门 安装 pip install pymongo 连接 实际就是实例化一个客户端对象,然后客户端对象中指定一个库作为库对象,库对象中的集合对象就是之后常用来执行操作的对象 1 ''' ...
- [php反序列化] CVE-2020-15148(Yii2 反序列化漏洞) 漏洞复现
漏洞影响范围 Yii2 < 2.0.38 环境搭建 Yii2.0.37 漏洞分析 首先定位到漏洞起始点 为什么是这儿?我们该怎么发现是某个类的某个函数?为什么不是其他函数? 一般是__destr ...
- oracle extract
select extract(year from systimestamp) year ,extract(month from systimestamp) month ...
- javaIO——输入输出流
字节流与字符流 File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成. 流的基本操作步骤: Step1:根据文件路径创建File类对象. Step2:根据字节流 ...
- ebs 初始化登陆
BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...
- OC-copy,单例
总结 编号 主题 内容 一 NSFileManager NSFileManager介绍/用法(常见的判断)/文件访问/文件操作 二 集合对象的内存管理 集合对象的内存管理/内存管理总结 三 *copy ...
- RHEL 6.5安装系统
转自如下链接: http://www.jb51.net/os/128752.html
- ActiveRecord教程
(一.ActiveRecord基础) ActiveRecord是Rails提供的一个对象关系映射(ORM)层,从这篇开始,我们来了解Active Record的一些基础内容,连接数据库,映射表,访问数 ...