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.

题意:给出一个数组,可能经过旋转的,并且数组中包含重复的元素。求其最小值

思路:用二分法。(1)如果a[mid] < a[right],说明最小值是在[left, mid]之间。(2)如果a[mid] > a[right],说明最小值在[mid+1,right]之间.(3)如果a[mid] = a[right],说明最小 值在[left, right-1]之间

代码如下:

class Solution {public int findMin(int[] nums){int left = 0, right = nums.length - 1;while (left < right){int mid = (left + right) >> 1;if (nums[mid] > nums[right]) left = mid + 1;else if (nums[mid] < nums[right]){right = mid;}else{right--;}}return nums[left];}
}

LeetCode Find Minimum in Rotated Sorted Array II相关推荐

  1. LeetCode——Find Minimum in Rotated Sorted Array II

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

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

  3. LeetCode Find Minimum in Rotated Sorted Array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  4. Find Minimum in Rotated Sorted Array II

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

  5. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

  6. leetcode 81 Search in Rotated Sorted Array II ----- java

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

  7. [LeetCode]Find Minimum in Rotated Sorted Array

    题目描述: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  8. [LeetCode] 81. Search in Rotated Sorted Array II

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  9. 154. Find Minimum in Rotated Sorted Array II

    文章目录 题目理解 二分+分治 只有二分 154是 153的升级版本. 题目理解 输入:一个按升序排序的数组nums,但是这个数组在某个位置被旋转了.(例如., 原始数组是[0,1,2,4,5,6,7 ...

最新文章

  1. 打印N个数组整体最大的TopK
  2. python取反函数_Python优雅的反函数int(string,base)
  3. Sql Server事务日志
  4. 160 - 25 CodeZero.1
  5. python 遗传算法 agv_基于改进遗传算法的AGV路径规划
  6. 高中学生计算机软件,中学生计算器
  7. iptables filter表案例/iptables nat表应用
  8. SSD——样本正负比控制+多尺度检测 (目标检测)(one-stage)(深度学习)(ECCV 2016)
  9. 算法知识点——(3)监督学习——SVM
  10. python-视频抽帧、数据集命名
  11. Solaris系统下修改IP地址
  12. css内联样式---动态绑定背景图片
  13. html图片白色背景怎么去掉,怎么把PPT图片的白色背景去掉 PPT去除图片背景颜色技巧...
  14. 高中英语语法(003)-句子的要素及种类
  15. w7电脑蓝屏怎么解决_Win7电脑突然蓝屏怎么办?
  16. 根据url读取html文件
  17. 电脑查服务器型号,怎么在服务器的操作系统中查看服务器的型号
  18. Unity 内置访问安卓权限
  19. Python求正负交错序列前N项和
  20. Oracle的下载与安装

热门文章

  1. firedac的TFDStoredProc动态创建并调用存储过程
  2. js 求两个日期之间相差天数
  3. 关于android开发环境中sdk和adt更新到22.6之后多了appcompat_v7
  4. [C#程序]汉字转拼音缩写的函数
  5. 如何用python创建一个下载网站-使用Python下载文件的简单示例
  6. python基础知识点-Python入门基础知识点(基础语法介绍)
  7. 适合自学python的图书-清华学霸整理,Python入门到精通这几本书帮你快速入行
  8. python论坛哪些好-好的python论坛
  9. 简明python教程百度云-网易云课堂 - 我的职业课堂
  10. python官网下载步骤linux-linux下安装python