我在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. hibernate多对多映射文件详解(一)

    1.仓库表属性 public class WarehouseNew implements java.io.Serializable { // Fields private static final l ...

  2. android图像模糊技术

    今天我们来更深入了解一下Android开发上的模糊技术.我读过几篇有关的文章,也在StackOverFlow上看过一些相关教程的帖子,所以我想在这里总结一下学到的东西. 为什么学习这个模糊技术? 现在 ...

  3. NSDate简单的使用

    NSDateFormatter *dateFormtter=[[NSDateFormatter alloc] init]; [dateFormtter setDateFormat:@"yyy ...

  4. unity3D对象的显示和隐藏

    SetActive/active/SetActiveRecursively 后两者比较旧,现在通常用第一个SetActive 必须先new一个gameobject对象用于实例化,然后再设置其activ ...

  5. sgu Kalevich Strikes Back

    这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. #include <cstdio> #include <cstring> #inc ...

  6. 三元运算符和GridView数据显示

    三元运算符嵌套使用:<%# Eval("InsertType").ToString() == "0" ? "数据库" : Eval(& ...

  7. Transposed Matrix

    Transposed Matrix In linear algebra, the transpose of a matrix A is another matrix AT (also written  ...

  8. 【转】vlc android 代码编译

    转自:http://blog.csdn.net/asircao/article/details/7734201 系统:ubuntu12.04代码:git://git.videolan.org/vlc- ...

  9. python高级编程 编写一个包1

    #目的是:编写,发行python包可重复过程"""1:是缩短开始真正工作之前所需要的设置时间,也就是提供模板2:提供编写包的标准化方法3:简化测试驱动开发方法的使用4:为 ...

  10. java的Future使用方法

    首先,Future是一个接口,该接口用来返回异步的结果. package com.itbuluoge.mythread; import java.util.ArrayList; import java ...