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. jsp action中附件下载的写法

    //一些主要的包和类 import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java ...

  2. 国内GIT托管服务

    http://www.cnblogs.com/TianFang/p/3348949.html

  3. Red Black Tree in C

    http://web.mit.edu/~emin/www.old/source_code/red_black_tree/index.html

  4. ecshop mobile 文件介绍

    mobile手机端 1.common位置:include\apps\default\common\ function.php show_message 成功跳转页面    其他页面引用  show_m ...

  5. json_encode详解,转义

    1.json_encod基本用法:数组转字符串 <?php $arr = array (,,,,); echo json_encode($arr); ?> 以上例程会输出: {,,,,} ...

  6. MySQL笔记-最简单的方法来解决找不到mysqld.sock文件的问题

    首先,环境:ubuntu 14.04,采用apt-get的方式安装的,手动安装可能路径设置稍有区别. 1.安装MySQL后,用命令行首次启动时发现找不到Mysqld.sock文件,提示: ERROR ...

  7. 不下载SDK启动Android Studio

    一.不下载SDK启动Android Studio 给一个解决方法:Android Studio安装目录下的bin,idea.properties:idea.properties文件末尾加一行:disa ...

  8. C-基本语法与运算

    编译: Technorati 标记: C 1, 编译compilers 命令make 将高级语言转换为低级语言. clang: 1,预处理(preprocessing) 2,编译(complition ...

  9. 浅谈JavaScript中的正则表达式

    引言 对于正则表达式我想作为程序员肯定使用过它,那天书般的表达方式,我用一次就记住它了.这篇博客先介绍一些正则表达式的内容,然后介绍JavaScript中对正则表达式特有的改进.下面开始介绍正则表达式 ...

  10. mapreduce 模板

    /*** * MapReduce Module * @author nele * */ public class ModuleMapReduce extends Configured implemen ...