题目:

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.

Example 1:

Input: [1,3,5]
Output: 1

Example 2:

Input: [2,2,2,0,1]
Output: 0

Note:

  • This is a follow up problem to Find Minimum in Rotated Sorted Array.
  • Would allow duplicates affect the run-time complexity? How and why?

分析:

这道题是153题的延申,153题链接在这里https://www.cnblogs.com/silentteller/p/11163491.html

多了一个限制,也就是允许重复元素出现,所以一次划分后就不会像153题那样出现一个有序数组和一个有可能有序数组的结果。

比如[3,3,3,3,1,3]划分后[3,3,3]和[3,1,3]两个数组均无序,所以要继续对这两个数组进行划分,而且当左右两端元素相同时,不能单纯的认为其内元素都相同,要继续划分,直到出现左端元素小于右端元素为止。

程序和153题相同,因为在判断数组是否有序的时候时严格判断左端值小于右端值的。

程序:

class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.size()==) return nums[];
int l = ;
int r = nums.size()-;
return find(nums, l, r);
}
int find(vector<int>& nums, int l, int r){
if(nums[l] < nums[r] || l==r) return nums[l];
if(r-l == ) return min(nums[l], nums[r]);
int mid = (l+r)/;
return min(find(nums, l, mid-), find(nums, mid, r));
}
};

LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)的更多相关文章

  1. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

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

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

  3. Java实现 LeetCode 154 寻找旋转排序数组中的最小值 II(二)

    154. 寻找旋转排序数组中的最小值 II 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找 ...

  4. [Swift]LeetCode154. 寻找旋转排序数组中的最小值 II | Find Minimum in Rotated Sorted Array II

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  5. [LeetCode] 154. 寻找旋转排序数组中的最小值 II

    题目链接 : https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目描述: 假设按照升序排序的数组在预 ...

  6. 154. 寻找旋转排序数组中的最小值 II

    转跳点:--\(˙<>˙)/-- 原本打算大年三十十一起写完的,结果这篇拖到了年初一…… 这道题比刚刚那道,麻烦一点,因为有重复,所以我们需要考虑重复的情况,就是刚刚的两种情况变成了三种: ...

  7. 154寻找旋转排序数组中的最小值II

    title: 寻找旋转排序数组中的最小值II 题目描述 题目链接:寻找旋转排序数组中的最小值II 解题思路 和上题同理:数组特点有 nums[mid] < nums[right],最小值肯定在m ...

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

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

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

随机推荐

  1. Potyczki Algorythmiczne 2019

    Runda próbna: A + B 设$f[i]$表示两数相加得到前$i$位的方案数,由$f[i-1]$和$f[i-2]$转移得到. #include<cstdio> #include ...

  2. C#获取CPU和内存使用率

    获取内存使用率 方式1: using System; using System.Runtime.InteropServices; namespace ConsoleApp1 { public clas ...

  3. React服务器端渲染框架next.js项目实战及部署上下文context问题解决办法

    在前段时间折腾过搭建react服务器端项目架构,点击这里了解下,正好最近有个重构PC网站的需求,考虑SEO等因素.在做技术选型的时候决定采用nextjs.项目开发过程中,nextjs的体验非常棒,个人 ...

  4. python中easydict的简单使用

    easydict的作用:EasyDict可以使得以属性的方式去访问字典的值! 1. 实例1:获取字典的值 2. 实例2: 设置属性 3. 在深度学习中往往利用easydict建立一个全局的变量

  5. 如何忽略Findbugs的bug

    如何忽略Findbugs的bug 除了用xml的形式去忽略一些文件和bug.最好用的还是注解: 下面的方法会有MT_CORRECTNESS和STYLE的bug.注解忽略方法为: @edu.umd.cs ...

  6. eclipse快速给表达式生成对应变量的快捷键

    这里记录下在Eclipse中快速给表达式生成对应变量的快捷键,有两种方式. [Ctrl + 2] 光标放在该表达式行的任意位置,按[Ctrl+2],会弹出提示,根据提示选择[F/L/R],就会自动生成 ...

  7. Python term 模块

    Python term 模块 term 模块适用于文字编辑,样式切换,光标移动 等一系列操作适用于终端命令行文字光标处理等操作. 安装 pip install py-term 使用方式 方式一 imp ...

  8. F#周报2019年第22期

    新闻 2019年实用F#挑战结果 FSharp.Formatting正在确定维护者 实用F#挑战的赢家们 使用F#在分布式系统中进行故障检测与共识 F#里的Cloudflare Worker Juni ...

  9. 基于Spark的电影推荐系统(推荐系统~4)

    第四部分-推荐系统-模型训练 本模块基于第3节 数据加工得到的训练集和测试集数据 做模型训练,最后得到一系列的模型,进而做 预测. 训练多个模型,取其中最好,即取RMSE(均方根误差)值最小的模型 说 ...

  10. Linux存储管理

    一.存储基础知识 从工作原理区分: 机械 HDD 固态 SSD SSD的优势: SSD是摒弃传统磁介质,采用电子存储介质进行数据存储和读取的一种技术,突破了传统机械硬盘的性能瓶颈,拥有极高的存储性能, ...