LeetCode(153) Find Minimum in Rotated Sorted Array
题目
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];
}
};
LeetCode(153) Find Minimum in Rotated Sorted Array的更多相关文章
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- (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 ...
- [LeetCode]题解(python):033-Search in Rotated Sorted Array
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated ...
- 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 ...
- 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. ( ...
- 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. ( ...
- Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array)
Leetcode之二分法专题-153. 寻找旋转排序数组中的最小值(Find Minimum in Rotated Sorted Array) 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
[LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
随机推荐
- Codeforces Round #396 (Div. 2) D
Mahmoud wants to write a new dictionary that contains n words and relations between them. There are ...
- 转 用好HugePage,告别Linux性能故障
超过32G 的数据库,可以是使用如下方法配置. ######### Slow Performance with High CPU Usage on 64-bit Linux with Large SG ...
- 记录一下在SpringBoot中实现简单的登录认证
代码参考博客: https://blog.csdn.net/weixin_37891479/article/details/79527641 在做学校的课设的时候,发现了安全的问题,就不怀好意的用户有 ...
- 112 Path Sum 路径总和
给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22, 5 / \ ...
- Kettle-Spoon入门示例
Spoon 是Kettle的设计调试工具 [Demo文档下载] https://files.cnblogs.com/files/shexunyu/Kettle-Spoon-Demo%E5%B8%AE% ...
- TruncateTable数据库清理
原文引用 https://www.cnblogs.com/liangxiaoking/p/5958456.html 本地链接下载 https://files.cnblogs.com/files/she ...
- Django分组查询
先补充两个知识点: 1.group by 大前提:可以按照任意字段分组,但是最好是按照分辨度比较低的来分组(重复比较多的分辨度比较低). group by分组可以单独使用,不搭配其他条件. 分组的字段 ...
- 为网站设置icon图标用于显示在浏览器标签页最左侧
icon图标,想必大家对它并不陌生吧,在浏览网页时会看到浏览器标签页的最左侧会有一个小图标,这个正是icon图标.本例为大家介绍下如何为网站设置这个图标 这句话起什么作用 ?复制代码 代码如下: &l ...
- osx launchpad图标的删除
安装了个parallels desktop之后,OSX中的launchpad中的图标多了不少,可是好多都不是我自己想要的,我们该怎么删除或者修改呢,下面介绍一些方法: ①直接操作Appications ...
- -bash: xx: command not found 在有yum源情况下处理
-bash: xx: command not found 在有yum源情况下处理 yum provides "*/xx" ###"xx"代表某命令 或者 yu ...