题目

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.

Subscribe to see which companies asked this question

分析

在含有重复元素的旋转有序序列中查找最小元素。

与上一题类似,LeetCode(153) Find Minimum in Rotated Sorted Array 153题中的旋转有序数组不包含重复元素,而此题允许重复元素,增加了一点难度。

我想题目重点考察的还是沿用二分查找的方法解决,思路参考

AC代码

class Solution {
public: //方法一:使用stl库函数
int findMin1(vector<int>& nums) {
if (nums.empty())
return 0; vector<int>::iterator iter = min_element(nums.begin(), nums.end());
return *iter;
} //方法二:整个数列除一处为最大值到最小值的跳转外,为两部分的递增
int findMin2(vector<int>& nums)
{
if (nums.empty())
return 0;
if (nums.size() == 1)
return nums[0];
for (size_t i = 1; i < nums.size(); ++i)
{
if (nums[i - 1] > nums[i])
return nums[i];
}//for
//没有找到跳转元素,则序列无旋转
return nums[0];
} int findMin(vector<int> &nums)
{
if (nums.empty())
return 0;
else if (nums.size() == 1)
return nums[0];
else{
int lhs = 0, rhs = nums.size() - 1; while (lhs < rhs && nums[lhs] >= nums[rhs])
{
int mid = (lhs + rhs) / 2;
if (nums[lhs] > nums[mid])
rhs = mid;
else if (nums[lhs] == nums[mid])
++lhs;
else
lhs = mid + 1;
}//while
return nums[lhs];
}
}
};

GitHub测试程序源码

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

  1. LeetCode(153) Find Minimum in Rotated Sorted Array

    题目 Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium Suppose a sorted array is rota ...

  2. Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

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

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

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

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

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

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

  7. 【刷题-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 ...

  8. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

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

  9. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

随机推荐

  1. js和jq中常见的各种位置距离之offset()和position()的区别(二)

    offset()返回的是相对于当前文档的坐标,position()返回的是相对于其定位的祖辈元素的坐标. 使用position()方法时事实上是把该元素当绝对定位来处理,获取的是该元素相当于最近的一个 ...

  2. (转)useradd用户,组管理案例

    原文:https://www.cnblogs.com/Csir/p/6403830.html?utm_source=itdadao&utm_medium=referral 示例:要求oldbo ...

  3. mysql 5.1 在Windows下重置root 用户密码

    在windows下:打开命令行窗口,停止mysql服务(这里不用进入mysql目录): net stop mysql 进入mysql安装目录的bin文件夹下 执行: mysqld --skip-gra ...

  4. MDX分页查询

    WITH SET [e16a30d0-2174-4874-8dae-a5085a75a3e2] as NONEMPTY({[Measures].[终端销售数量], [Measures].[终端销售吊牌 ...

  5. ef 操作 mysql 中文乱码问题

    1.保证mysql数据的编码为utf8 启动mysql mysql -hlocalhost -uroot -p 输入密码 show VARIABLES like 'character_%'; SET  ...

  6. (办公)ssm发送邮件

    1.添加jar包 <!-- Javamail API --> <dependency> <groupId>javax.mail</groupId> &l ...

  7. Kendo MVVM 数据绑定(四) Disabled/Enabled

    Kendo MVVM 数据绑定(四) Disabled/Enabled Disabled 和 Enabled 绑定可以根据 ViewModel 的某个属性值的 true,false 来设置 DOM 元 ...

  8. MySQL memory引擎 table is full 问题处理

    解决mysql的内存表“table is full”错误   101209 13:13:32 [ERROR] /usr/local/mysql/bin/mysqld: The table ‘test_ ...

  9. Python各种参数类型

    1. Python的参数传递是值传递还是引 举例说明Python函数参数传递的几种形式,并说明函数传参是值传递还是引用传递 一.位置参数 调用函数时根据函数定义的参数位置来传递参数.例子: def p ...

  10. XML(可拓展标记语言)

      XML 是可扩展标记语言(Extensible Markup Language)的缩写,其中的 标记(markup)是关键部分.您可以创建内容,然后使用限定标记标记它,从而使每个单词.短语或块成为 ...