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. JQuery------$.ajax()的使用方法

    菜鸟教程地址: http://www.runoob.com/jquery/ajax-ajax.html html(../Home/Index.cshtml) <body> <butt ...

  2. mysqli连接数据库的模板

    <?php $host="localhost"; $db_user="root"; //数据库用户 $db_pass=""; //数据 ...

  3. oracle表字段为汉字,依据拼音排序

    在order by后面使用NLSSORT函数转化汉字列,如下 select * from student order by NLSSORT(name,'NLS_SORT=SCHINESE_PINYIN ...

  4. ast模块

    有这么一个需求,你想从文件中读取字典,方法有很多,这里用的是ast模块 import ast with open("account","r",encoding= ...

  5. C# JIT & AOT

    http://msdn.microsoft.com/library/z1zx9t92 http://msdn.microsoft.com/en-us/library/ht8ecch6(v=vs.90) ...

  6. ecshop 后台【左侧新增菜单】

    模板文件admin/template/menu.htm admin/includes/inc_menu.php 菜单排序(链接) langagues/zh_cn/admin/common.php  语 ...

  7. JS中的 公有变量、私有变量 !

    公有变量.私有变量 ! 初学者的见解,算是记录学习过程,也算是分享以便共同成长,如有不正确的地方,还请不吝赐教! 先看代码1: function car(){ var wheel = 3; //私有变 ...

  8. OC-封装

    一.      set方法和get方法 1.          set方法和get方法的使用场合 @public的成员可以被随意赋值,应该使用set方法和get方法来管理成员的访问(类似机场的安检.水 ...

  9. PHP7的安装

    PHP7和HHVM比较PHP7的在真实场景的性能确实已经和HHVM相当, 在一些场景甚至超过了HHVM.HHVM的运维复杂, 是多线程模型, 这就代表着如果一个线程导致crash了, 那么整个服务就挂 ...

  10. Flex入门笔记

    Test_01.mxml <?xml version="1.0" encoding="utf-8"?> <viewer:BaseWidget  ...