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. 解决:HTTP 错误 404.2 - Not Found. 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    错误重现: 在发布网站的过程中,虽然不是第一次发布了,但是还是遇到了很多的问题.为了以后可以轻松解决此类问题还是积累下来比较好. 问题:HTTP 错误 404.2 - Not Found. 由于 We ...

  2. [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python

    原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...

  3. 使用ECharts实现数据图表分析

    一.ECharts介绍 实现对统计数据的图形分析之前用过JFreeChar,但它是用纯java实现编码繁琐且效果不佳,后来又使用过Fusioncharts 报表工具,它是基于Flash的图表组件.以X ...

  4. Oracle Agile PLM Web Services 的实现

    Oracle 的产品Agile PLM内置了许多Web Services,其他系统可以通过Web Servcies实现对Agile PLM系统资源的访问.快速学会使用的方法,是去Oracle的官网下载 ...

  5. Idea代码可视化插件

    Idea代码可视化插件 https://plugins.jetbrains.com/plugin/7324-code-iris

  6. Android动态加入控件约束位置

    用LayoutParams: RelativeLayout insertLayout = (RelativeLayout)view1.findViewById(R.id.screen);//scree ...

  7. mac 下vim 配置文件

    " Configuration file for vim set modelines=0 " CVE-2007-2438 " Normally we use vim-ex ...

  8. 自定义Lisp透明命令

    我们知道在CAD中,如果我们在命令前面加一个单引号,则为透明命令.透明命令就是一个命令还没结束,中间插入另一个命令,然后继续完成前一个命令.插入的命令即透明命令,插入透明命令是为了更方便的完成第一个命 ...

  9. android中的数据库操作(SQLite)

    android中的数据库操作 android中的应用开发很难避免不去使用数据库,这次就和大家聊聊android中的数据库操作. 一.android内的数据库的基础知识介绍 1.用了什么数据库   an ...

  10. 触发器二(DML触发器)(学习笔记)

    DML触发器(语句触发器) 由DML语句进行触发,当用户执行了INSERT,UPDATE,DELETE操作时就会触发操作 示例一.只有在每个月的10日才允许办理,新员工入职与离职,其他时间不允许增加和 ...