我在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. C++类中的静态成员变量与静态成员函数

    最近一直看c++相关的项目,但总是会被c++类中的静态成员变量与静态成员函数的理解感觉很是模糊,不明白为什么类中要是用静态成员变量.于是在网上搜集了一些资料,自己再稍微总结下. 静态成员的概念: 静态 ...

  2. 用css实现一个空心圆,并始终放置在浏览器窗口左下角

    用css实现一个空心圆,并始终放置在浏览器窗口左下角         div{                 position:fixed;                 bottom:0;   ...

  3. 第一次碰到try-except(core python programming 2nd Edition 3.6)

    # coding: utf-8 # 使用Windows系统,首行'#!/usr/bin/env Pyton'无用,全部改为'# coding: utf-8' 'readtextfile.py -- r ...

  4. ng-bind-html 的使用

    AngualrJS 提供了指令ng-bind-html 用于绑定包含HTML标签的文档,使用方式: <ANY ng-bind-html=""> ... </ANY ...

  5. cf B. Simple Molecules

    http://codeforces.com/contest/344/problem/B #include <cstdio> #include <cstring> using n ...

  6. 考查嵌入式C开发人员的最好的16道题

    约定:   1) 下面的测试题中,认为所有必须的头文件都已经正确的包含了    2)数据类型             char 一个字节 1 byte        int 两个字节 2 byte ( ...

  7. uniq详解

    一.简介 报告或删除文件中重复的行. uniq 命令读取由 InFile 参数指定的标准输入或文件.该命令首先比较相邻的行,然后除去第二行和该行的后续副本.重复的行一定相邻.(在发出 uniq 命令之 ...

  8. 【czy系列赛】czy的后宫4 && bzoj1925 [Sdoi2010]地精部落

    [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到-N之间的整数.他 ...

  9. error Infos

  10. 从运维角度浅谈 MySQL 数据库优化

    一个成熟的数据库架构并不是一开始设计就具备高可用.高伸缩等特性的,它是随着用户量的增加,基础架构才逐渐完善.这篇博文主要谈MySQL数据库发展周期中所面临的问题及优化方案,暂且抛开前端应用不说,大致分 ...