我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步。

题目:

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.

Tag:

Array; Binary Search

体会:

常规binary search, 但不同于先前去找insert position的那道题。先前那道题要找的target可能是不出现在array中,但是这道题target一定出现在array中,所以循环条件相应改变了,变成当low和high指向同一个index时就停止了。

1. 每次在去找中点值之前,如果当前A[low] - A[high]已经是sorted的部分了,就可以直接返回A[low]了。

2. 在跳动指针的时候注意,如果是A[mid] > A[high], 指针low可以直接跳过mid,i.e. low = mid + 1, 这是因为既然A[mid]比A[high]大了,那mid上的一定不会是最小值了。相对应的,如果是A[mid] <= A[high]的情况,就不能跳过mid, 因为从这个不等式来看,mid是可能成为最小值的,所以只能有 high = mid.

 class Solution {
public:
int findMin(vector<int> &num) {
int low = ;
int high = num.size() - ;
// binary search ends when low and high
// points to the same index
while (low < high) {
// already found the sorted part?
if (num[low] < num[high]) {
return num[low];
}
int mid = low + (high - low) / ;
if (num[mid] > num[high]) {
// since num[mid] > num[high],
// num[mid] cannot the minimum, we can
// jump to mid + 1 safely.
low = mid + ;
} else {
// num[mid] <= num[high],
            //so mid might be the position we want
// we can not skip mid
high = mid;
}
}
return num[low];
}
};

[Leetcode] Find the minimum in rotated sorted array的更多相关文章

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

  2. 【leetcode】Find Minimum in Rotated Sorted Array I&&II

    题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...

  3. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  4. [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

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

  6. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  7. [LeetCode#154]Find Minimum in Rotated Sorted Array II

    The question: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are ...

  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] 154. Find Minimum in Rotated Sorted Array II_Hard

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

  10. leetcode 【 Find Minimum in Rotated Sorted Array II 】python 实现

    题目: Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? W ...

随机推荐

  1. java基础知识再学习--集合框架-对象的强、软、弱和虚引用

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://zhangjunhd.blog.51cto.com/113473/53092 本文 ...

  2. iscroll-lite.js源码注释

    /*! iScroll v5.1.2 ~ (c) 2008-2014 Matteo Spinelli ~ http://cubiq.org/license */ (function (window, ...

  3. redis之lua脚本

    背景介绍 redis数据库提供了一些管理功能比如 流水线:打包发送多条命令,并在一个回复里面接收所有被执行命令的结果.事务:一次执行多条命令,被执行的命令要么就全部都被执行,要么就一个也不执行.并且事 ...

  4. 【面试】Spring问答Top 25

    本文由 ImportNew - 一直在路上 翻译自 howtodoinjava.欢迎加入翻译小组.转载请见文末要求. 本人收集了一些在大家在面试时被经常问及的关于Spring的主要问题,这些问题有可能 ...

  5. UVA 12169 Disgruntled Judge

    我该怎么说这道题呢...说简单其实也简单,就枚举模拟,开始卡了好久,今天看到这题没a又写了遍,看似会超时的代码交上去a了,果然实践是检验真理的唯一标准... #include <iostream ...

  6. KEIL里 Volatile的用法

    volatile用于防止相关变量被优化. 例如对外部寄存器的读写.对有些外部设备的寄存器来说,读写操作可能都会引发一定硬件操作,但是如果不加volatile,编译器会把这些寄存器作为普通变量处理,例如 ...

  7. Auto Install Workflow Manager 1.0

    Write-Host "- Begining Download Service Bus..." Start /W "c:\Program Files\Microsoft\ ...

  8. Network Attack

    Network Attack Nicola regularly inspects the local networks for security issues. He uses a smart and ...

  9. QQ聊天界面的布局和设计(IOS篇)-第一季

    我写的源文件整个工程会再第二季中发上来~,存在百度网盘, 感兴趣的童鞋, 可以关注我的博客更新,到时自己去下载~.喵~~~ QQChat Layout - 第一季 一.准备工作 1.将假数据messa ...

  10. OpenMeetings(3)----启动顺序解析

    OpenMeetings系统较大,代码量也不小,如果对前端的OpenLaszlo开发不熟悉的话,刚研究代码时,确实有种丈二和尚摸不着头脑的感觉.一番研究之后,终于初步理清了系统的初步动作流程,具体执行 ...