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的更多相关文章

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

  2. [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. ...

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

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

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

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

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

  6. [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. ...

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

  9. 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. ( ...

随机推荐

  1. 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心

    2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...

  2. 安装nginx环境(含lua)时遇到报错ngx_http_lua_common.h:20:20: error: luajit.h: No such file or directory的解决

    下面是安装nginx+lua环境时使用的相关模块及版本,ngx_devel_kit和lua-nginx-module模块用的都是github上最新的模块.并进行了LuaJIT的安装. #Install ...

  3. Python3和Python2 异常处理except的不同

    最近准备做个微信公众号的项目,但是微信平台的开发者文档介绍的是web.py,虽然有支持python3的版本.但是在介绍页面的还是python2的代码. python2.x的时候: try: raise ...

  4. 压缩及解压命令gzip、bzip2、tar

    1. gzip 描述:压缩与解压缩 用法:gzip[选项]...[文件名称]... 选项:-d 解压 gzip hello.txt     # 文件压缩后名为hello.txt.gz gzip -d ...

  5. JS数组扁平化(flat)

    需求:多维数组=>一维数组 let ary = [1, [2, [3, [4, 5]]], 6]; let str = JSON.stringify(ary); 第0种处理:直接的调用 arr_ ...

  6. linux下用vim写Python自动缩进的配置

    #首先用 find / -name vimrc 找到vimrc文件#一般在 /etc/vimrc#进入vimrc后加入以下命令 set number set autoindent set shiftw ...

  7. exception内置对象

    当当前页面可能发生异常的时候,此页面将此异常交给另外一个页面处理 在page处添加    errorPage="处理异常的页面.jsp" 在异常处理的页面的page处应该添加  i ...

  8. LeetCode 741. Cherry Pickup

    原题链接在这里:https://leetcode.com/problems/cherry-pickup/ 题目: In a N x N grid representing a field of che ...

  9. 正确创建本地C++发布构建PDBS

    在调试版本中遇到的一个问题是编译本地的C++应用程序.例如,许多局部变量消失了,因为代码生成器没有将它们放在堆栈上,而是将它们放在寄存器中,就像在调试生成中发生的那样.此外,release积极地构建对 ...

  10. 【区间dp】P1063 能量项链

    一道区间dp的水题 题目链接 来快活啊! 思路 很简单的区间dp,思路和floyed差不多,就是需要把项链处理成环形 用\(f[l][r]\)表示以\(a[l]\)开头\(a[r]\)结尾的数串的最大 ...