[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 this affect the run-time complexity? How and why?
Suppose a sorted array 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.
153. Find Minimum in Rotated Sorted Array 的拓展,这个题里允许有重复的元素。原来是依靠中间和边缘元素的大小关系,来判断哪一半是有序的。而现在因为重复元素的出现,如果遇到中间和边缘相等的情况,就无法判断哪边有序,因为哪边都有可能有序。假设原数组是{1,2,3,3,3,3,3},那么旋转之后有可能是{3,3,3,3,3,1,2},或者{3,1,2,3,3,3,3},判断左边缘和中心的时候都是3,就不知道应该截掉哪一半。解决的办法是对边缘移动一步,直到边缘和中间不在相等或者相遇,这就导致了会有不能切去一半的可能。所以最坏情况就会出现每次移动一步,总共移动n,算法的时间复杂度j: O(logn) ~ O(n)。
Java:
public int findMin(int[] num) {
if(num == null || num.length==0)
return 0;
int l = 0;
int r = num.length-1;
int min = num[0];
while(l<r-1)
{
int m = (l+r)/2;
if(num[l]<num[m])
{
min = Math.min(num[l],min);
l = m+1;
}
else if(num[l]>num[m])
{
min = Math.min(num[m],min);
r = m-1;
}
else
{
l++;
}
}
min = Math.min(num[r],min);
min = Math.min(num[l],min);
return min;
}
Python:
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums) - 1
while left < right:
mid = left + (right - left) / 2 if nums[mid] == nums[right]:
right -= 1
elif nums[mid] < nums[right]:
right = mid
else:
left = mid + 1 return nums[left]
Python:
class Solution2(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left, right = 0, len(nums) - 1
while left < right and nums[left] >= nums[right]:
mid = left + (right - left) / 2 if nums[mid] == nums[left]:
left += 1
elif nums[mid] < nums[left]:
right = mid
else:
left = mid + 1 return nums[left]
C++:
class Solution {
public:
int findMin(vector<int> &nums) {
if (nums.empty()) return 0;
int left = 0, right = nums.size() - 1, res = nums[0];
while (left < right - 1) {
int mid = left + (right - left) / 2;
if (nums[left] < nums[mid]) {
res = min(res, nums[left]);
left = mid + 1;
} else if (nums[left] > nums[mid]) {
res = min(res, nums[right]);
right = mid;
} else ++left;
}
res = min(res, nums[left]);
res = min(res, nums[right]);
return res;
}
};
类似题目:
[LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索
[LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
[LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
All LeetCode Questions List 题目汇总
[LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II的更多相关文章
- LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [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. ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 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_Hard
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 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. ( ...
- 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(H)(P)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
随机推荐
- AbsInt — 确保代码安全的性能/资源分析工具套件
德国AbsInt公司是一家安全苛求软件研发.确认.验证和认证工具链的供应商,能够为客户提供完整的确保代码安全的性能分析工具套件以及软件分析.验证.确认和编译器技术相关咨询服务.AbsI ...
- C# CefSharp如何在Winforms应用程序中使用
最近做了一个很小的功能,在网页上面打开应用程序,用vs的debug调试,可以正常打开应用程序,可布置到iis上面却无法运行应用程序,吾百度之,说是iis权限问题,吾依理做之,可怎么折腾也不行.最后bo ...
- 【转】TUN/TAP虚拟网络设备
转: 原文:https://www.cnblogs.com/wyzhou/p/9286864.html ------------------------------------------------ ...
- 《The One 团队》:第九次团队作业:BETA冲刺与团队项目验收
项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报告:(3) ...
- pycharm——常用快捷键操作
编辑类(Editing): Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 类名完成Ctrl + Shift + Enter 语句完成Ctrl + P ...
- vue之组件通信
vue组件通信一般分为以下几种情况: 1.父子组件通信: 2.兄弟组件通信: 3.跨多层级组件通信: 一.父子通信 父组件通过props传递数据给子组件,子组件通过emit发送事件传递数 ...
- EventWaitHandle 第一课
本篇通过一个列子使用EventWaitHandle实现两个线程的同步.请参看下面的列子. using System; using System.Collections.Generic; using S ...
- nodejs之MongoDB安装[windows平台]
下载MongoDB,本为下载msi文件安装,下载地址 下载完成之后直接双击文件安装,安装时注意安装路径 创建一个空文件夹,用于存放数据库文件 通过控制台进入安装目录下的bin目录,或者在bin ...
- C博客作业01——分支,顺序结构
C博客作业01--分支,顺序结构 0.展示PTA总分 1本章学习内容 1.1学习内容总结 1)格式化输出函数printf(),scanf(). 它是什么? 对于初学者而言,一开始了解接触它们,只是被硬 ...
- 「2019-8-13提高模拟赛」树 (tree)
传送门 Description 你有一个 \(n\)个点的树,第 \(i\)个点的父亲是\(p_i\).每个点有一个权值 \(t_i\) 和一个颜色黑或者白.所有点一开始都是白色. 你要进行 \(m\ ...