【Leetcode】【Medium】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
might become 4 5 6 7 0 1 2
).
Find the minimum element.
You may assume no duplicate exists in the array.
解题思路:
一眼就看出o(n)的解法,所以题目是为了考察用二分搜索实现o(logn)的解法;
当num[start] < num[end]时,说明原数列没有旋转,或者整旋转了一圈回到了原点,所以num[start]是最小值;
同理,只要是首比尾小,都表示有序,那么最小值一定在乱序里,这样就可以用mid去中间值,做判断,每次缩小一半范围;
注意:
不用每次都用递归,使用start和end表示操作的下标就可以了;
中值mid = (start+end)/2,mid的值可能等于start,但是不会等于end,所以每次start=mid+1 或者 end = mid,就一定能保证每次更新下标值,不会陷入死循环;
代码:
class Solution {
public:
int findMin(vector<int> &num) {
int start = ;
int end = num.size() - ; while (start < end) {
if (num[start] < num[end])
return num[start];
int mid = (start + end) / ;
if (num[start] <= num[mid])
start = mid + ;
else
end = mid;
} return num[start];
}
};
【Leetcode】【Medium】Find Minimum in Rotated Sorted Array的更多相关文章
- 【leetcode】Find Minimum in Rotated Sorted Array I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- 【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 ...
- 【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 ...
- leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现
题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...
- 【刷题-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 ...
随机推荐
- 如何在vue中请求本地json文件
1..修改webpack.base.conf.js 文件中添加'/static': resolve('static'),如下所示,此时存放于static的json文件就可以通过/static/xxx. ...
- Linux下MySQL出现乱码的解决方法
开发环境 cent os 6.5 mysql springboot duird 故障描述 本地开发环境没有任何问题,上传到服务器后发现提交的表单内容只要是中文直接变成 ??? 解决方式 错误尝试: 一 ...
- 【CSS】布局之选项卡与图片库
前面对简单的选项卡和简单的图片库进行了实现,现在把两者结合起来,实现下面这样的效果. 现在附上代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4 ...
- 江铖:乳腺癌识别By AI
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由云加社区技术沙龙 发表于云+社区专栏 演讲嘉宾:江铖,腾讯觅影高级研究员.多年以来一直从事计算机视觉相关的研究.加入腾讯以后,负责腾讯 ...
- Java调用打印机打印指定路径图片
依赖 javax.print package com.xgt.util; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; ...
- Gradient Optimization
Gradient Optimization Gradient Descent Batch Gradient Descent Mini-Batch Gradient Descent Stochastic ...
- Firebird3 embedded connection
Firebird3 的嵌入式连接和以前的版本不同,官方也不再单独发布嵌入式版本了,因为嵌入式版本已经包含在zip包里了. 具体只需要文件: fbclient.dll.ib_util.dll.icudt ...
- js 常用脚本
1.判断电话号码和手机号码 var tel = $("#tel").val(); if (isNotBlank($.trim(tel))) { //不为空的情况下判断符合手机号码标 ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...
- MySql数据库与JDBC编程二
DML语法语句:主要操作数据表中的数据,完成插入新数据,修改已有数据,删除不要的数据的任务 1,insert into 语句 用于向指定表插入数据,一次只能插入一条记录:insert into tab ...