[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. ( ...
随机推荐
- 关于 ES5 & ES6 数组遍历的方法
ES5 数组遍历方法 1.for 循环 , , , , ] ; i < arr.length; i++) { console.log(arr[i]) } 2.forEach , , , , ] ...
- mac 使用 brew 安装 nginx 及各种命令
一.安装 brew install nginx 或 sudo brew install nginx 二.启动 brew services start nginx 或 sudo brew service ...
- ASP.NET Core ---- 系列文章
(13)ASP.NET Core 中的选项模式(Options) (12)ASP.NET Core 中的配置二(Configuration) (11)ASP.NET Core 中的配置一(Config ...
- Python tkinter模块弹出窗口及传值回到主窗口操作详解
这篇文章主要介绍了Python tkinter模块弹出窗口及传值回到主窗口操作,结合实例形式分析了Python使用tkinter模块实现的弹出窗口及参数传递相关操作技巧,需要的朋友可以参考下 本文实例 ...
- 走,去出海,一起“Copy to World” | 36氪出海行业报告
http://www.sohu.com/a/200845344_114778 从工具类产品在海外聚集大量流量到新闻.社交游戏等内容类产品在海外取得优异成绩,中国正在完成从Copy to China向C ...
- Codeforces Round #605 (Div. 3) B. Snow Walking Robot(构造)
链接: https://codeforces.com/contest/1272/problem/B 题意: Recently you have bought a snow walking robot ...
- SQL Server 父子迭代查询语句,树状查询
这个也有用: -- Get childs by parent idWITH TreeAS( SELECT Id,ParentId FROM dbo.Node P WHERE P.Id = 21 -- ...
- Tensorflow细节-Tensorboard可视化-简介
先搞点基础的 注意注意注意,这里虽然很基础,但是代码应注意: 1.从writer开始后边就错开了 2.writer后可以直接接writer.close,也就是说可以: writer = tf.summ ...
- BZOJ 4477: [Jsoi2015]字符串树 可持久化trie树
这个是真——可持久化字典树..... code: #include <bits/stdc++.h> #define N 100006 #define setIO(s) freopen(s& ...
- learning scala list.collect
collect will apply a partial function to all elements of a Traversable and return a different collec ...