Suppose an array sorted in ascending order 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.

思路:有重复元素的时候,不能按I中这么分三类(反例:Input: [3,3,1,3]=>将roate in right判断成没有rotate)。解决方法是,当碰到nums[start]=nums[end]的情况时,end-1,寻找不同元素再进行二分法。

class Solution {
public:
int findMin(vector<int>& nums) {
return dfs(nums,,nums.size()-);
} int dfs(vector<int>& nums, int start, int end){
if(start==end) return nums[start];
if(nums[start]==nums[end]) return dfs(nums,start, end-); int mid = start + ((end - start) >> );
if(nums[mid] > nums[end]){ //rotate in the right
return dfs(nums, mid+,end);
}
else if(nums[start] <= nums[mid]){ //no rotate
return nums[start];
}
else{ //rorate in the left
return dfs(nums, start, mid);
}
}
};

154. Find Minimum in Rotated Sorted Array II (Array; Divide-and-Conquer)的更多相关文章

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

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

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

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

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

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

  6. 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II

    Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...

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

  8. 154 Find Minimum in Rotated Sorted Array II

    多写限制条件可以加快调试速度. ======= Follow up for "Find Minimum in Rotated Sorted Array":What if dupli ...

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

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

  10. LeetCode OJ 154. Find Minimum in Rotated Sorted Array II

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

随机推荐

  1. Redis 五大数据类型及常用操作

    # 更详细的操作命令请查看 => http://redisdoc.com/ 1: KEY ( 键值 )  => 常见操作 2: String ( 字符串 ), 最大支持 512M 2.1: ...

  2. jsp取addFlashAttribute值深入理解即springMVC发redirect传隐藏参数

    结论:两种方式 a.如果没有进行action转发,在页面中el需要${sessionScope['org.springframework.web.servlet.support.SessionFlas ...

  3. 高级js--(面向对象js,arguments,闭包,自调)

    1. Arguments对象    l检测参数个数 1.在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. function howManyArgs() { al ...

  4. CSS多行文字垂直居中的两种方法

    之前写过一篇关于:CSS左右居中对齐的文章,里面提到的两种方法其实也可以引申为垂直居中对齐.写这篇文章是因为要兼容IE6.IE7的问题,我们都知道一行文字时可以通过line-height来设置垂直居中 ...

  5. WPF按钮响应函数中执行操作耗时较长时,UI 界面不能实时更新——问题原因与解决方案

    原因: 一般来说,一个WPF窗口程序,只有一个UI线程, 如果这个线程停在某个函数,UI将会被阻塞,所有其他的界面操作都不能即时响应. 解决方案: 新开一个线程来执行耗时较长的操作,以不阻塞UI.

  6. Java快速开发平台,JEECG 3.7.6性能增强版本发布

    JEECG 3.7.6 性能增强版本发布 导读       ⊙Vue SPA单页面应用 ⊙Datagrid标签实现不同风格切换,支持BootstrapTable.EasyUI ⊙灵活通用代码生成器工厂 ...

  7. Python集合的基本操作

    #-*coding:utf-8 -* list =set([2,3,4]) list2 =set([5,3,7]) #交集 #print (list.intersection(list2)) #并集 ...

  8. lientDataset的Delta与XML相互转换

    一个ClientDataset的Delta与XML相互转换的文章:大家都知道TClientDataSet的Delta属性保存数据集的变化,但是Delta是OleVariant类型的属性,这样如果用De ...

  9. Linux命令:zip

    语法: zip   [选项]   zip文件  源文件s   选项 全称 含义 举例 -r recursive 递归压缩子目录里的文件(包括子目录里的子目录) zip   -r    target.z ...

  10. How to Pronounce the Word OR

    How to Pronounce the Word OR Share Tweet Share Tagged With: OR Reduction Study the OR reduction.  Th ...