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., 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.
class Solution {
public:
int findMin(vector<int> &num) {
int start = ;
int end = num.size() - ;
int mid = ;
while (start < end) { //注意这里和普通的二分查找不同,这里是start < end不是 start <= end.
mid = start + (end - start)/;
if (num[mid] > num[end])
start = mid + ; //此时可以扔掉mid的值
else
end = mid;//此时不能扔掉mid的值
}
return num[end]; //退出循环说明start与end相等,所以只剩一个元素可能,所以return [start]或者return [end]都可以了。
//注意不能return mid,可以从{2,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 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寻找旋转排序数组中的最小值 (C++)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- 语句调优基础知识-set statistics profile on
set statistics profile on 获取语句真实的执行计划信息 set statistics profile on go select distinct Productid,unitp ...
- 确认是否是因为做了物理I/O而导致的性能不佳
要获取语句是否进行了I/O,需要打开set statistics on 和set statistics on.
- c/c++ const关键字
const 在星号的右边:不可以改指针的指向,可以用指针改里面的值 int * const p; const在星号的左边:可以改指针的指向,不可以用指针改里面的值 int const *p; cons ...
- strlen strcat strcpy strcmp 自己实现
strlen strcat strcpy strcmp 自己实现 strlen include <stdio.h> #include <string.h> #include & ...
- 详解PHP中的过滤器(Filter)
PHP 过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入. 什么是 PHP 过滤器? PHP 过滤器用于验证和过滤来自非安全来源的数据. 验证和过滤用户输入或自定义数据是任何 Web 应用程序 ...
- Docker: dockerfile常用关键字
Dockerfile指令 Dockfile执行和shell命令一行,一行一行执行- 写Dockerfile注意点: 1. 尽量少RUN 2. 多个命令拼接在一起 ...
- CSS 简介、 选择器、组合选择器
#CSS 装饰器引入<!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...
- vue-cli脚手架搭建的项目怎么去除eslint验证
修改webpack.base.conf.js这个基础配置文件了.具体修改方法如下: module: { rules: [ ...(config.dev.useEslint ? [createLinti ...
- JavaScript数组对象详情
Array 数组概述 Array 类型是 ECMAScript 最常用的类型.javaScript 中的 Array 类型和其他语言中的数组有着很大的区别. 虽然数组都是有序排列,但 javaScri ...
- C#、Java中的一些小知识点总结(持续更新......)
前言:在项目中,有时候一些小的知识,总是容易让人忽略,但是这些功能加在项目中往往十分的有用,因此笔者在这里总结项目中遇到的一些实用的小知识点,以备用,并持续更新...... 1.禁用DataGridV ...