Question

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

Solution

抓住数组的特点。

Code

class Solution {
public:
int findMin(vector<int>& nums) {
int size = nums.size();
if (size == 1)
return nums[0];
int low = 0;
int high = size - 1;
while (low < high) {
// 没用递归,所以注意求中间节点的方法
int mid = low + (high - low) / 2;
// 如果更小,那么会比右边的都小
if (nums[mid] < nums[high])
high = mid;
// 如果更大,那最小的肯定在右边
else if (nums[mid] > nums[high])
low = mid + 1;
// 如果相等,不知道最大值在左边还是在右边
else
high--;
}
return nums[low];
}
};

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

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  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. NSArray最简单的倒序

    NSArray里有 sortedArrayUsingSelector:等排序的方法,但是最简单的倒序排列的方法如下: NSArray *deArray = [[keyArrays reverseObj ...

  2. 1044: Access denied for user 'hehe'@'localhost' to database 'imooc'

    当我使用 mysql授予用户时, GRANT ALL PRIVILEGES ON *.* TO hehe IDENTIFIED BY 'some' WITH GRANT OPTION; 出现:1044 ...

  3. glibc-2.23_int_malloc_流程浅析

  4. Windows上安装zabbix客户端

    1.下载解压 https://www.zabbix.com/downloads/3.4.0/zabbix_agents_3.4.0.win.zip conf目录下放有agent配置文件 bin目录下有 ...

  5. mysql联合其他表做更新

    在sql server中,我们可是使用以下update语句对表进行更新: update a set a.xx= (select yy from b) where a.id = b.id ; 但是在my ...

  6. Java compiler level does not match解决方法, java 修改编译用的jdk的方法

    从别的地方导入一个项目的时候,经常会遇到eclipse/Myeclipse报Description  Resource Path Location Type Java compiler level d ...

  7. bash: ./a.sh: /bin/bash^M: bad interpreter: No such file or directory的解决方法------dos--->unix

    一些人喜欢用vim来写linux shell script, 但是, 有的人喜欢在Windows下用一些方便的编辑器(比如鼎鼎大名的Notepad++)写好, 然后拷贝文件到linux下, 结果呢, ...

  8. C++学习笔记--名称空间

    名称空间是为了更好的控制名称的作用域,以管理不同的类库,避免发生冲突. 1.创建名称空间 如下,使用namespace关键字创建了pers和debts两个名称空间. #ifndef NAMESP_H_ ...

  9. PHP生成名片、网址二维码

    PHP生成名片.网址二维码 php生成名片(vcard)二维码: <?php$vname = 'test';  $vtel = '13800000000';  generateQRfromGoo ...

  10. PATH_INFO, SCRIPT_NAME, REQUEST_URI区别示例