Suppose an array sorted in ascending order 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.


题目标签:Array, Binary Search

  题目给了我们一个 旋转有序 数组,让我们找到最小值。如果是在还没有旋转的情况下,那么我们知道,最小值就是第一个数字。

  那么当遇到旋转的情况呢,我们就需要找到那个 “断点” 的两个数字,换句话说,就是最大值和最小值,而且这两个数字一定是相邻的。

  我们来看一下例子:

  0  1  2  4  5  6  7  直接返回第一个数字

  1  2  4  5  6  7  0  返回7 和0 中小的那个数字

  2  4  5  6  7  0  1  

  4  5  6  7  0  1  2

  5  6  7  0  1  2  4

  6  7  0  1  2  4  5

  7  0  1  2  4  5  6

  利用二分法来找到 最大和最小的 两个数字。

  rule 1: 如果 mid 小于 left, 意味着最大的数字在左边,继续去左边找,把right = mid, 这里要包括mid,因为mid 可能是最小的数字;

  rule 2: 如果 mid 大于 right, 意味着最小的数字在右边,继续去右边找,把left = mid, 这里要包括mid, 因为mid 可能是最大的数字。

  当left 和right 相邻的时候,返回小的数字就可以了。

 

Java Solution:

Runtime beats 53.07%

完成日期:08/30/2017

关键词:Array, Binary search

关键点:旋转中,min 和 max 必然是相邻的,利用二分法找到min 和max

 class Solution
{
public int findMin(int[] nums)
{
if(nums.length == 1)
return nums[0]; int left = 0;
int right = nums.length - 1; if(nums[left] < nums[right])
return nums[0]; while(left + 1 != right)
{
int mid = left + (right - left) / 2; if(nums[mid] < nums[left]) // if left is greater than mid, move to left
right = mid;
else if(nums[mid] > nums[right]) // if mid one is greater than right, move to right
left = mid;
} // if there are only two number
return Math.min(nums[left], nums[right]);
}
}

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)的更多相关文章

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

  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. [LeetCode] 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] Search 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 ...

  5. [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  6. Leetcode153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中最小值

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...

  7. [CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

    11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code ...

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

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

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

随机推荐

  1. JavaScript随笔

    文档模式 主要模式2中混杂模式和标准模式. 1混杂模式,混杂模式会让IE的行为与(包含非标准特性的)IE5相同. 2标准模式,标准模式让IE的行为更接近标准行为. 准标准模式:通过过渡型或框架集型触发 ...

  2. SpringSecurity 登录 - 以及Md5加密

    我们现在开放一个链接给其他系统,来访问我们的系统 http://localhost:8080/hulk-teller-web/haihui!init.jspa?loginId=teller01& ...

  3. vue+element搭建的后台管理系统

    最近工作不是很忙,自己在学习vue,在网上找了一个简单的项目练练手..... 这是本人的gitHub 上的项目地址:https://github.com/shixiaoyanyan/vue-admin ...

  4. vue webuploader 组件开发

    最近项目中需要用到百度的webuploader大文件的分片上传,对接后端的fastdfs,于是着手写了这个文件上传的小插件,步骤很简单,但是其中猜到的坑也不少,详细如下: 一.封装组件 引入百度提供的 ...

  5. GCD之Apply

    dispatch_apply函数是dispatch_sync函数和dispatch_group的结合体.该函数将按指定的次数将指定的block追加到指定的dispatch queue中,并等待全部处理 ...

  6. Tensorflow之卷积神经网络(CNN)

    前馈神经网络的弊端 前一篇文章介绍过MNIST,是采用的前馈神经网络的结构,这种结构有一个很大的弊端,就是提供的样本必须面面俱到,否则就容易出现预测失败.如下图: 同样是在一个图片中找圆形,如果左边为 ...

  7. 开博近一年的感想 by 程序员小白

    /* 好吧,这里的写博客应该理解为更宏观的写文章. */   在去年的这个时候,我所知道的平台只有 CSDN 和博客园..然而 CSDN 的广告实在是不想吐槽了,选择博客园是一件非常自然的事情.要说开 ...

  8. QT的安装及环境配置

    QT的安装及环境配置 一.windows的下QT的安装及环境配置 (一)从框架安装程序中安装 步骤: 准备:下载QT库,下载指定版本的MINGW,QT IDE 1.下载QT安装文件如:qt-win-o ...

  9. 使用docker部署standalone cinder

    | 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 背景 OpenSta ...

  10. Windows下memcached的安装配置

    下载windows 32位或64位 memcached 下载 memcached_dll 1.将第一个包解压放某个盘下面,比如在c:\memcached.2.在终端(也即cmd命令界面)下输入 'c: ...