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 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

Search in Rotated Sorted Array,Search in Rotated Sorted Array II,Find Minimum in Rotated Sorted Array II对照看

解法一:暴力解法,直接使用algorithm库中的求最小元素函数

需要遍历整个vector

class Solution {
public:
int findMin(vector<int> &num) {
if(num.empty())
return ;
vector<int>::iterator iter = min_element(num.begin(), num.end());
return *iter;
}
};

解法二:利用sorted这个信息。如果平移过,则会出现一个gap,也就是从最大元素到最小元素的跳转。如果没有跳转,则说明没有平移。

比上个解法可以省掉不少时间,平均情况下不用遍历vector了。

class Solution {
public:
int findMin(vector<int> &num) {
if(num.empty())
return ;
else if(num.size() == )
return num[];
else
{
for(vector<int>::size_type st = ; st < num.size(); st ++)
{
if(num[st-] > num[st])
return num[st];
}
return num[];
}
}
};

解法三:二分查找

Search in Rotated Sorted Array对照来看

Search in Rotated Sorted Array题中是二分查找最大值,而本题是二分查找最小值。

class Solution {
public:
int findMin(vector<int>& nums) {
if(nums.empty())
return ;
if(nums.size() == )
return nums[];
int n = nums.size();
int low = ;
int high = n-;
while(low < high && nums[low] > nums[high])
{
int mid = low + (high-low)/;
if(nums[mid] < nums[low]) // mid is in second part
high = mid;
else if(nums[mid] == nums[low]) // since (low<high)-->(low+1==high)
return nums[high]; // nums[low]>nums[high]
else
low = mid+;
}
return nums[low];
}
};

【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)的更多相关文章

  1. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  2. 【leetcode】153. Find Minimum in Rotated Sorted Array

    Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example ...

  3. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  4. 【刷题-LeetCode】153 Find Minimum in Rotated Sorted Array

    Find Minimum in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some p ...

  5. 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  6. 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II

    Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...

  7. 【Lintcode】159.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  ...

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

  9. 【原创】leetCodeOj --- Find Minimum in Rotated Sorted Array II 解题报告

    题目地址: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目内容: Suppose a sort ...

随机推荐

  1. 【BZOJ】【2120】数颜色 & 【2453】维护队列

    莫队算法 分块大法吼 这题乍一看跟HH的项链很像啊……只是多了一个修改操作……然而我就不会做了

  2. UVA 400 (13.08.05)

     Unix ls  The computer company you work for is introducing a brand new computer line and is developi ...

  3. JavaScript中定义对象的四种方式

    最近在阅读< JavaScript 高级程序设计>,未免遗忘读过的内容,就打算以博客的形式做些读书笔记.今天介绍的是 JavaScript 中的四种定义对象的方法,除了这四种方法,还有工厂 ...

  4. CSS写的提示框(兼容火狐IE等各大浏览器)

    项目上使用jQuery的Tooltip组件,在谷歌上正常,在火狐和IE下没有效果,所以根据谷歌的提示框单独用CSS写了个提示框,比较好的兼容了火狐和IE,且效果一样 原Tooltip代码: $('#d ...

  5. word 文档如何加密

    给Word文档加密主要有以下几个方法:文件加密文件菜单设置:1.打开需要加密的Word文档.2.选“文件”的“另存为”,出现“另存为”对话框,在“工具”中选“常规选项”,出现“保存”选项卡.3.分别在 ...

  6. 申请红帽企业版Linux开发者订阅

    导读 注册成为开发者计划的成员现在可以得到一套免费的 Red Hat Enterprise Linux 许可证,RHEL 开发套件将为程序员提供一个构建企业应用的稳定发展平台.红帽开发订阅成员还可以免 ...

  7. [转载]C++中处理XML文件

      写Unmanaged Code在.NET时代成为一种很悲惨的事,当你需要处理XML文件时,这种感觉会变得尤其强烈.FCL中的System.XML多简单啊,连Steve Ballmer都知道怎么用. ...

  8. yeoman-angular-gulp

    1.安装 yeoman npm install -g yo gulp bower 2.安装genarate-angular //npm install generator-angular npm in ...

  9. Word 代码高亮

    整理文档比较费事,提供个脚本放在VBA里,使Word 代码高亮的一种方法是改变颜色 'script to high light code In document Private Function is ...

  10. [CSS] Collapsing Margins

    Refactor the spacing between <header>, <article>, and <aside> so that elements wil ...