【刷题-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 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的更多相关文章
- 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 ...
- [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. ...
- 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 ...
- 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. ( ...
- 【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 ...
- 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 ...
- 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 ...
- 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. ...
- 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. ...
- 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. ( ...
随机推荐
- Java 中接口和抽象类的 7 大区别!
本文已收录<Java常见面试题>:https://gitee.com/mydb/interview Java 是一门面向对象的编程语言,面向对象的编程语言有四大特征:抽象.封装.继承和 ...
- 面试官问我TCP三次握手和四次挥手,我真的是
候选者:面试官你好,请问面试可以开始了吗 面试官:嗯,开始吧 面试官:今天来聊聊TCP吧,TCP的各个状态还有印象吗? 候选者:还有些许印象的,要不我就来简单说下TCP的三次握手和四次挥手的流程吧 候 ...
- 人工智能论文解读精选 | PRGC:一种新的联合关系抽取模型
NLP论文解读 原创•作者 | 小欣 论文标题:PRGC: Potential Relation and Global Correspondence Based Joint Relational ...
- 【LeetCode】200. Number of Islands 岛屿数量
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】82. Remove Duplicates from Sorted List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/remove-du ...
- 牛客练习赛39 B:选点(二叉树遍历+LIS)
链接: https://ac.nowcoder.com/acm/contest/368/B 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言262 ...
- 【C\C++笔记】register寄存器关键字
使用寄存器变量提高运行速度 1未使用寄存器组 #include<stdio.h> int main(){ unsigned long a=0; for(int i=0;i<10000 ...
- js处理复杂数据格式数组嵌套对象,对象嵌套数组,reduce处理数据格式
let list=[ {id:1,name:'a'}, {id:1,name:'b'}, {id:1,name:'c'}, {id:2,name:'A'}, {id:2,name:'B'}, {id: ...
- Vue的安装及使用(Vue的三种安装使用方式)
vue是一个JavaMVVM库,是一套用于构建用户界面的渐进式框架,是初创项目的首选前端框架.它是以数据驱动和组件化的思想构建的,采用自底向上增量开发的设计.它是轻量级的,它有很多独立的功能或库,我们 ...
- Java初学者作业——声明变量储存商品信息并进行输出
返回本章节 返回作业目录 需求说明: 声明变量存储商品信息(商品名称.商品价格和商品库存数量). 输出商品信息. 实现思路: 打印商品商品信息实现步骤: 声明变量存储商品信息.为变量赋值. 输出变量的 ...