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 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
分析:
这道题给出了一个有序数组,找出数组中最小的元素,其实就是找出第一个小于最后一个元素出现的位置,
比如题中给出的 4 5 6 0 1 2 ,
最后一个元素为2,
只需要找出第一个小于2的元素出现的位置。
之所以不用第一个元素作为target,因为翻转数组有一种极限情况,那就是完全不翻转,就是0 1 4 5 6 7,
这种情况下如果用第一个元素作为target就会出错。
// find the first element that <= targrt, the last element is the target.
public class Solution {
// find the first element that <= targrt, the last element is the target.
public int findMin(int[] nums) {
if (nums.length == 0){
return 0;
}
int start = 0, end = nums.length;
int target = nums[nums.length - 1];
int mid;
while (start + 1 < end){
mid = start + (end - start) / 2;
if (nums[mid] <= target){
end = mid;
}else{
start = mid;
}
}
if (nums[start] <= target){
return nums[start];
}else{
return nums[end];
}
}
}
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 --------- 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. ( ...
- [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 ...
随机推荐
- springMVC的注解详解
springmvc常用注解标签详解 1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业 ...
- 深入JVM-锁与并发
一.锁在Java虚拟机中的实现与优化 1.1 偏向锁 偏向锁是JDK 1.6 提出的一种锁优化方式.其核心思想是,如果程序没有竞争,则取消之前已经取得锁的线程同步操作.也就说,若某一锁被线程获取后,便 ...
- 简单说说Tk和Tcl
开园第一个博客,简单说说Tk和Tcl. 我接触Tk和Tcl是在学习Python Tkinter时候,创建Tk对象,下面言归正传: Tcl:工具命令语言,英文全称为Tool Command Langua ...
- Arcgis 几何网络分析
ArcGIS:网络分析(转) 由于之前对网络分析的理解有很多问题,所以在网上找了一些资料,这是其中一篇我觉得比较好的,所以就整理了一下,发到网上来,留个底吧,呵呵 注:关于几何网络的建立见前面的& ...
- php 设计模式 例子
加载类:include("./Ren.class.php");include "./Ren.class.php"; require("./Ren.cl ...
- curl方式创建elasticsearch的mapping
curl方式创建elasticsearch的mapping curl -XPUT 'http://192.168.1.105:9200/bank/item2/_mapping' -d '{ " ...
- Ubuntu下apt-get命令详解
在Ubuntu下,apt-get近乎是最常用的shell命令之一了,因为他是Ubuntu通过新立得安装软件的常用工具命令. 本文列举了常用的APT命令参数: apt-cache search pack ...
- 兼容IE的写法收集||bug修复
这篇文章实时更新 属于IE的专属写法 其中,S表示Standards Mode即标准模式,Q表示Quirks Mode,即兼容模式 hack 示例 IE6(S) IE6(Q) IE7(S) IE7(Q ...
- 简单CSS布局留用
1.导航栏固定显示代码,文字居中,z-index header{ position: fixed; top: 0px; left: 10%; width: 80%; height: 80px; bor ...
- 全选,全不选,反选的js实现
全选练习 ** 使用复选框上面一个属性判断是否选中 - checked属性 - checked=true:选中 ...