题目

Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium

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.

分析

二分搜索的扩展应用。

寻找一个有序序列经旋转后的最小元素。

关键是掌握好,有序序列旋转后的元素之间的关系。

AC代码

class Solution {
public:
int findMin(vector<int>& nums) {
if (nums.empty())
return INT_MIN; //元素个数
int size = nums.size(); int lhs = 0, rhs = size - 1;
while (lhs <= rhs)
{
//递增状态,返回低位值
if (nums[lhs] <= nums[rhs])
return nums[lhs];
//相邻,返回较小值
else if (rhs - lhs == 1)
return nums[lhs] < nums[rhs] ? nums[lhs] : nums[rhs]; //判断子序列
int mid = (lhs + rhs) / 2;
//右侧递增,则最小值位于左半部分
if (nums[mid] < nums[rhs])
{
rhs = mid;
}
//否则,最小值位于右半部分
else{
lhs = mid + 1;
}
}//while
} //优化函数
int findMin2(vector<int>& nums)
{
if (nums.size() == 1)
return nums[0]; int lhs = 0, rhs = nums.size() - 1; while (nums[lhs] > nums[rhs])
{
int mid = (lhs + rhs) / 2;
if (nums[mid] > nums[rhs])
lhs = mid + 1;
else
rhs = mid;
}//while
return nums[lhs];
}
};

GitHub测试程序源码

LeetCode(153) Find Minimum in Rotated Sorted Array的更多相关文章

  1. LeetCode(154) Find Minimum in Rotated Sorted Array II

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

  2. (LeetCode 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 ...

  3. [LeetCode]题解(python):033-Search in Rotated Sorted Array

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated ...

  4. leetcode解题报告(3):Search 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 m ...

  5. LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)

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

  6. LeetCode 154.Find Minimum in Rotated Sorted Array II(H)(P)

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

  7. Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array)

    Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array) 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ...

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

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

  9. Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

随机推荐

  1. pip 的简单安装与基本使用

    pip 是 Python 著名的包管理工具,在 Python 开发中必不可少.本文只介绍各平台最新可用并且最简单的 pip 安装方式,以方便初学者和不会敲代码只需通过 pip 安装特定工具的小伙伴们. ...

  2. python入门之生成器

    生成器 通过列表生成式,可以直接创建一个列表,但是,受到内存限制,列表容量肯定是有限得.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果仅仅需要访问前面几个元素,那后面绝大多数元素 ...

  3. Windows下打开某些软件时显示显卡驱动不是最新的问题

    在Windows下打开某些对显卡要求比较高的软件时,会出现某些显卡驱动不是最新,要求更新到最新的提示,但是当你真的去更新显卡驱动的时候,却发现现在的显卡驱动已经是最新了,那么为什么还会有这样的提示呢, ...

  4. 使用express+mongoDB搭建多人博客 学习(6)发表文章

    发表文章 1.在modules文件夹下新建post.js var mongodb=require("./db"); function Post(name,title,post){ ...

  5. csu 1551: Longest Increasing Subsequence Again BIT + 思维

    预处理last[i]表示以第i个开始,的合法后缀. pre[i]表示以第i个结尾,的合法前缀. 那么每一个数a[i],肯定是一个合法后缀last[i] + 一个合法前缀,那么合法前缀的数字要小于a[i ...

  6. 绘制复杂的原理图元件和pcb封装库用于cadence(一)

    绘制TI公司的TPS53319电源芯片封装 由于产品设计需要大电流电源供电,选用TI公司TPS53319电源芯片通过cadence软件进行电路设计,但是TI公司所提供的封装格式为CAD File(.b ...

  7. kafka客户端发布record(消息)

    kafka客户端发布record(消息)到kafka集群. 新的生产者是线程安全的,在线程之间共享单个生产者实例,通常单例比多个实例要快. 一个简单的例子,使用producer发送一个有序的key/v ...

  8. 安装drupal对服务器环境的要求

    Drupal 8 Apache 2.x PHP 5.5.9 及以上版本 MySQL 5.5.3 及以上版本 Drupal 7 Apache 2.x PHP 5.2.5 及以上版本(推荐 PHP 5.4 ...

  9. leetcode128 Longest Consecutive Sequence

    思路: 维护一个unordered_map,key是每个连续区间的端点,value是该区间的长度. 实现: class Solution { public: int longestConsecutiv ...

  10. SQL 转换函数

    1.字符串与字符串相加 字符串相加   得到的是拼接成一列的字符串类型 例如 select name+code from car       name是nvarchar  code也是nvarchar ...