原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-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.

题解:

Find Minimum in Rotated Sorted Array的扩展, 这里有duplicates.

时间复杂度出现了变化,原来可以根据nums[mid] 和 nums[r]的大小关系比较,判断rotate部分是在左还是在右. 依据此来判断下一步是左面查找还是右面查找,现在若是nums[l] == nums[mid]时无法判断。

Time Complexity: O(n). Space: O(1).

AC Java:

 class Solution {
public int findMin(int[] nums) {
if(nums == null || nums.length == 0){
throw new IllegalArgumentException("Input array is null or empty.");
} int l = 0;
int r = nums.length-1;
while(l<r){
int mid = l+(r-l)/2;
if(nums[mid]<nums[r]){
r=mid;
}else if(nums[mid]>nums[r]){
l=mid+1;
}else{
r--;
}
} return nums[r];
}
}

LeetCode Find Minimum in Rotated Sorted Array II的更多相关文章

  1. [LeetCode] Find Minimum in Rotated Sorted Array 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

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

  3. [leetcode]Find Minimum in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...

  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. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

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

  7. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  8. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  9. 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

随机推荐

  1. 【液晶模块系列基础视频】4.2.X-GUI图形界面库-画矩形函数简介

    [液晶模块系列基础视频]4.2.X-GUI图形界面库-画矩形函数简介 ============================== 技术论坛:http://www.eeschool.org 博客地址: ...

  2. Xampp 添加 SSL

    我的 XAMPP 没有找到这句话 ,直接添加 extension=php_openssl.dll  大概988行另外,需要配置 httpd-ssl.conf 文件(*\xampp\apache\con ...

  3. VS插件开发

    参考资料: VS插件开发 - 个性化VS IDE编辑器 自己动手编写一个VS插件(一) VS Addin插件基本开发入门 VS Addin插件配置.部署

  4. ptmalloc2源码解析初探

    本文是徽沪一郞在学习华庭(庄明强)所撰<glibc内存管理-ptmalloc2源代码分析>的阅读笔记.本笔记以slides的方式加以呈现.文件采用latex+tikz编辑而成,如果对lat ...

  5. PHP 使用 GET 传递数组变量

    PHP 代码: <?php print_r($_GET['tag_name']); URL: http://127.0.0.16/get.php?tag_name[]=送货快&tag_n ...

  6. dom4j最常用最简单的方法

    要使用dom4j读写XML文档,需要先下载dom4j包,dom4j官方网站在 http://www.dom4j.org/目前最新dom4j包下载地址:http://nchc.dl.sourceforg ...

  7. java ReentrantReadWriteLock

    // read and write lock is mutual exclusion lock //Listing 7-3. Using ReadWriteLock to Satisfy a Dict ...

  8. vimtutor

    ================================================================================ 欢 迎 阅 读 < V I M ...

  9. android导入项目出现R文件不能生成

    关于原因网上有好多,比如 1.有时候eclipse不自动编译,把project clean一下,让R.java重新生成   2.选择菜单  Project >> Clean ,前提是勾选上 ...

  10. Abstract Algebra chapter 7

    7.7:Encrypt each of the following RSA messages x so that x is divided into blocks of integers of len ...