1. Find Minimum in Rotated Sorted Array

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.

You may assume no duplicate exists in the array.

Example 1:

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

Example 2:

Input: [4,5,6,7,0,1,2]
Output: 0

二分查找

class Solution {
public:
int findMin(vector<int>& nums) {
// 特殊情况
if(nums.size() == 1)return nums[0];
int l = 0, r = nums.size()-1, mid;
if(nums[r] > nums[l])return nums[l];
//查找
while(l <= r){
mid = (l+r)/2;
//两种情况对应着断点
if(nums[mid] > nums[mid+1])return nums[mid+1];
if(nums[mid-1] > nums[mid])return nums[mid];
//调整两侧的范围
if(nums[mid] > nums[0]){
l = mid + 1;
}else{
r = mid - 1;
}
}
return -1;
}
};

【刷题-LeetCode】153 Find Minimum in Rotated Sorted Array的更多相关文章

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

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

  3. Java for LeetCode 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 ...

  4. LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)

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

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

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

  7. leetcode 153. Find Minimum in Rotated Sorted Array --------- java

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

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

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

  10. LeetCode 153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中的最小值 (C++)

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

随机推荐

  1. CF1569A Balanced Substring 题解

    Content 给定一个长度为 \(n\) 且仅包含字符 a.b 的字符串 \(s\).请找出任意一个使得 a.b 数量相等的 \(s\) 的子串并输出其起始位置和终止位置.如果不存在请输出 -1 - ...

  2. 5、双指针技巧套路框架——Go语言版

    前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...

  3. Python3 第五周大纲(模块,导入方法、import本质,导入优化,模块的分类)

    1.定义: 模块:逻辑上组织python代码(变量.函数.类.逻辑:实现一个功能,本质是.py结尾的文件) 2.导入方法 import module_name,module_name2,...... ...

  4. Kafka安装Kafka-Eagle可视化界面

    要先安装jdk 可以参考:https://www.cnblogs.com/pxblog/p/10512886.html 下载 http://download.kafka-eagle.org/ 上传到服 ...

  5. 微信公众号开发用户授权登录报"redirect_uri 参数错误"错误

    微信公众号开发 授权获取用户信息报错 "redirect_uri 参数错误" 出现这个情况要检查下 微信公众号配置了网页授权域名 在这里配置微信公众号redirect_uri中的域 ...

  6. 分享一下java需要的一些技术

    1.前言 you are 大哥,老衲很佩服你们_.还是一样的,有我联系方式的人,哪些半吊子不知道要学习哪些技术,一天让我整知识点,老衲也有事情做的,哪有那么多时间来一直搞知识点啊,我的博客更新很慢的, ...

  7. 【LeetCode】777. Swap Adjacent in LR String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 智商题 相似题目 参考资料 日期 题目地址:http ...

  8. mybatis 内部定义对象和集合

    mapper 配置文件中  引入两个重要的标签:association和collection标签.

  9. 使用 DML语句,对 “锦图网” 数据进行操作,聚合函数练习

    查看本章节 查看作业目录 需求说明: 根据客户 ID 统计订单数.订单总金额.最高订单金额.最低订单金额和每份订单平均金额,并按订单总金额升序显示 根据客户统计订单总订购人次数> 5 的统计信息 ...

  10. 【MySQL作业】连接查询综合应用——美和易思连接查询综合应用习题

    点击打开所使用到的数据库>>> 1.统计每件商品的销售数量和销售金额,要求按照销售量和销售金额升序显示商品名.销售量和销售金额, 由于需要统计每件商品的销售数量和销售金额,即便某种商 ...