原题链接在这里: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. C#开发MySQL数据库程序时需要注意的几点

    一:引用MySQL使用基于Parameter方式代码,总是提示:“Column '列名'cannot be null”解决 MySQL使用基于Parameter方式代码,总是提示:“Column '列 ...

  2. java网络编程之UDP实例

    package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgram { pu ...

  3. DotNetBar RibbonControl 控件动态添加项

    想做个插件式开发,界面用Dotnetbar的RibbonControl,需要通过代码动态的向RibbonControl控件添加项 示例代码如下: RibbonTabItem rti = new Rib ...

  4. 移动web app开发小贴士 收藏有用

    1 创建主屏幕图标 (Creating a home screen icon ,for ios)   1 2 3 4 5 6 //57*57 <link rel="apple-touc ...

  5. Scrum会议1

    小组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.16 会议内容: 1.连连看游戏选取图片素材. 2.连连看的界面设计 3.连连看具体的功 ...

  6. GDUT——1169: Krito的讨伐(优先队列BFS)

    1169: Krito的讨伐 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 619  Solved: 102 Description Krito终于干 ...

  7. Javascript 笔记与总结(1-4)this

    js 中函数的 4 种调用方式: ① 作为普通函数来调用,this 的值指向 window,准确地说,this 为 null,被解释成为 window.在 ECMAScript5 标准中,如果 thi ...

  8. PHP 开发 APP 接口 学习笔记与总结 - APP 接口实例 [5] 版本设计分析及数据表设计

    APP 版本升级以及 APP 演示 ① 版本升级分析以及数据表设计 ② 版本升级接口开发以及 APP 演示 /** * version_upgrade 版本升级信息表 */ CREATE TABLE ...

  9. 使用 html5 postMessage 实现跨域

    英文原文 中文翻译 因为web的安全机制,浏览器的同源策略.在不同域之间做数据交换就会涉及到跨域.A域如果要实现向B域发关消息,多多少少要有对B域有一定控制权,最起码人家B域要接收你的消息啊. 最近发 ...

  10. Front-end Developer Interview Questions

    Front-end-Developer-Interview-Questions A list of helpful front-end related questions you can use to ...