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的更多相关文章

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

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

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

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

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

随机推荐

  1. GMap.NET使用一

    https://greatmaps.codeplex.com/releases/view/20235 从上面网站下载需要的组件dll,也可以下载源码研究,解压后有两个文件夹,如图1所示,根据不同的fr ...

  2. Euclidean Space

    http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm

  3. js控制网页滚动条往下滚动

    function aa(i){ var tm = setInterval(function(){ var t = $(window).scrollTop(); , -) : Math.max((i-t ...

  4. Quartz.NET总结(三)Quartz 配置

    前两篇文章,已经介绍了Quartz.NET的使用和Cron表达式表达式的写法,今天说一说Quartz的配置,Quartz相关的配置有三个quartz.config.quartz_jobs.xml.lo ...

  5. TCP的三次握手与四次分手

    TCP的位置 TCP工作在网络OSI的七层模型中的第四层——Transport层,IP在第三层——Network层,ARP在第二层——Data Link层: 在第二层上的数据,我们把它叫Frame,在 ...

  6. yii2 小技巧

    参考地址:http://www.cnblogs.com/sandea/p/5714830.html 1.不通过日志获取AR执行的原生SQL语句和打印变量数据 $query = User::find() ...

  7. [Bug]IIs Cannot read configuration file due to insufficient permissions

    摘要 在部署站点的时候,遇到这样的问题Cannot read configuration file due to insufficient permissions 解决办法 在服务器上部署站点,浏览的 ...

  8. WinForm中新开一个线程操作 窗体上的控件(跨线程操作控件)

    最近在做一个winform的小软件(抢票的...).登录窗体要从远程web页面获取一些数据,为了不阻塞登录窗体的显示,开了一个线程去加载数据远程的数据,会报一个错误"线程间操作无效: 从不是 ...

  9. start: Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart:

    用Ubuntu远程登录虚拟host时出现:    start: Unable to connect to Upstart: Failed to connect to socket /com/ubunt ...

  10. 如何解决winows启动后出现grub?

    village :村庄, 村民 villa: 别墅 setting: 设置; ** 环境, 背景, 布置, 布局, 底座 what's the setting of the villa like? h ...