Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
  • [4,5,6,7,0,1,2] if it was rotated 4 times.
  • [0,1,2,4,5,6,7] if it was rotated 7 times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
You must write an algorithm that runs in O(log n) time.
 
class Solution {
public:
int findMin(vector<int>& nums) { //O(logn 的时间复杂度 感觉是二分法的检索方式) left>right 的话 最小值就在这里面 这样二分检索
//二分检索 中间的边界比较麻烦
int n=nums.size();
if(nums[0]<=nums[n-1]) return nums[0];
int left=0,right=n-1;
while(left<right-1)
{
int mid=(left+right)/2;
if(nums[left]>nums[mid])
{
//left=left;
right=mid;
}
else if(nums[mid]>nums[right])
{
left=mid;
//right=right
}
else if(nums[left]==nums[mid])
{
left=mid+1;
}
else if(nums[right]==nums[mid])
{
right=mid-1;
}
}
return min(nums[right],nums[left]); }
};

【leetcode】153. Find Minimum in Rotated Sorted Array的更多相关文章

  1. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

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

  2. 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)

    Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...

  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】153 Find Minimum in Rotated Sorted Array

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

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

  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. 【Lintcode】159.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  ...

  8. LeetCode OJ 153. 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 ...

  9. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

随机推荐

  1. buff/cache 占用过高解决方法

    cache 读磁盘时,数据从磁盘读出后,暂留在缓冲区(cache),为后续程序的使用做准备 buffer 写磁盘时,先保存到磁盘缓冲区(buffer),然后再写入到磁盘 三条命令: #echo 1 & ...

  2. jmeter压测IP欺骗绕过服务端限流

    1.环境声明 jmeter3.0 后端为内网环境 2.检查内网闲置的ip 工具地址,无需复杂安装,解压点击就可以用啦~~ https://pan.baidu.com/s/1Yzs1vezfFMoy-m ...

  3. mysql 免密码登录

    mysql 8免密码登录 UPDATE mysql.user SET authentication_string=null WHERE User='root'; FLUSH PRIVILEGES; e ...

  4. python实现图像二值化

    1.什么是图像二值化 彩色图像: 有blue,green,red三个通道,取值范围均为0-255 灰度图:只有一个通道0-255,所以一共有256种颜色 二值图像:只有两种颜色,黑色和白色,二值化就是 ...

  5. MySQL用limit代替SQL Server :top

    mysql 中不支持top,而是用limit代替 若要查询前10条记录,mysql用limit 10 LIMIT可以实现top N查询,也可以实现M至N(某一段)的记录查询,具体语法如下: SELEC ...

  6. [loj6051]PATH

    (不妨将下标改为从1开始) 参考loj2265中关于杨表的相关知识 构造一个$n$行且第$i$行有$a_{i}$个格子的杨表,依次记录其每一次增加的时间(范围为$[1,\sum_{i=1}^{n}a_ ...

  7. [gym102900H]Rice Arrangement

    (可以先阅读题目中关于顺逆时针的定义,避免理解错误) 考虑一盘菜$b_{i}$被$a_{j}$吃掉,对于其最后一次移动:如果是顺时针,则称$b_{i}$的移动区间为$[a_{j},b_{i}]$(若$ ...

  8. [gym102832J]Abstract Painting

    考虑每一个圆即对应于区间$[x_{i}-r_{i},x_{i}+r_{i}]$,可以看作对于每一个区间,要求所有右端点严格比其小的区间不严格包含左端点 用$f_{i}$表示仅考虑右端点不超过$i$的区 ...

  9. [atARC084F]XorShift

    如果异或变为加法和减法,那么根据扩欧,$k$合法当且仅当$k|\gcd_{i=1}^{n}a_{i}$ 换一种方式定义约数:$x$是$y$的约数当且仅当存在$p_{i}\in \{0,1\}$使得$\ ...

  10. Taro 3.4 beta 发布: 支持 Preact 为应用开辟更多体积空间

    项目体积是困扰小程序开发者的一大问题,如果开发者使用 Taro React 进行开发,更是不得不引入接近 100K 的 React 相关依赖,这让项目体积变得更加捉襟见肘.因此,Taro v3.4 的 ...