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. Java多线程高并发学习笔记(三)——深入理解线程池

    线程池最核心的一个类:ThreadPoolExecutor. 看一下该类的构造器: public ThreadPoolExecutor(int paramInt1, int paramInt2, lo ...

  2. Apache2.4 + Tomcat7 负载均衡配置

    一.配置tomcat 多启动 1.下载免安装版 tomcat7 http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.81/bin/apache-t ...

  3. 利用GPU实现无尽草地的实时渲染

    0x00 前言 在游戏中展现一个写实的田园场景时,草地的渲染是必不可少的,而一提到高效率的渲染草地,很多人都会想起GPU Gems第七章 <Chapter 7. Rendering Countl ...

  4. Opencv的使用,NDK的简单使用

    第一部分:安装运行: 1.下载opencv,并解压,将其目录下的sdk复制到eclipse的工作区间目录下,重命名为OpenCV-SDK(随意命名): 2.从eclipse中导入:file->i ...

  5. Maximum 贪心

    Maximum Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Des ...

  6. C#进阶之AOP

    一.AOP概念(转自) AOP(Aspect-Oriented Programming,面向切面的编程),它是可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技 ...

  7. JS脚本检查密码强度

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  8. Hexo + GitHub Pages搭建博客

    搭建 Node.js 环境 为什么要搭建 Node.js 环境? – 因为 Hexo 博客系统是基于 Node.js 编写的 Node.js 是一个基于 Chrome V8 引擎的 JavaScrip ...

  9. 我的第一个python web开发框架(6)——第一个Hello World

    小白中午听完老菜讲的那些话后一直在思考,可想来想去还是一头雾水,晕晕呼呼的一知半解,到最后还是想不明白,心想:老大讲的太高深了,只能听懂一半半,看来只能先记下来,将明白的先做,不明白的等以后遇到再学. ...

  10. ZOJ1315

    代码先寄放这里 #include<cstdio> #include<cstdlib> #include<iostream> #include<cstring& ...