[LeetCode] 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.
#include <iostream>
#include <vector>
using namespace std; class Solution {
public:
int findMin(vector<int> &num) {
int n = num.size();
if(n<||num[]<num[n-]) return num[];
int lft=,rgt=n-,mid=n-;
while(lft+<rgt){
if(num[mid-]>num[mid]) break;
mid = (lft+ rgt)/;
cout<<lft<<" "<<mid<<" "<<rgt<<endl;
if(num[lft]<num[mid]) lft=mid;
else rgt=mid;
mid = rgt;
}
return num[mid];
}
}; int main()
{
vector<int> num{,,,,,};
Solution sol;
cout<<sol.findMin(num)<<endl;
return ;
}
[LeetCode] Find Minimum in Rotated Sorted Array 二分搜索的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [LeetCode] 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 Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- 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 migh ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- Leetcode Find Minimum in Rotated Sorted Array I and II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode——Find Minimum in Rotated Sorted Array
Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...
随机推荐
- python 写 组合两两组合
紧挨着 组合 a b c d ----> ab ,bc ,cd portList = ['a', 'b', 'c', 'd'] for i, p in enumerate(portList) ...
- MySQL的备份与恢复理解与备份策略
MySQL的备份主要分为逻辑备份和物理备份 逻辑备份 在MySQL中逻辑备份的最大优点是对各种存储引擎都可以用同样的方法来备份.而物理备份则不同,不同的存储引擎有着不同的备份方法.Mysql中的逻辑备 ...
- 八、MySQL 数据类型
MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...
- tcl之list操作-lappend/lsearch/lsort/concat/split/join/
- B - Sonya and Exhibition CodeForces - 1004B (思维题)
B. Sonya and Exhibition time limit per test 1 second memory limit per test 256 megabytes input stand ...
- poj 3045 叠罗汉问题 贪心算法
题意:将n头牛叠起来,每头牛的力气 s体重 w 倒下的风险是身上的牛的体重的和减去s 求最稳的罗汉倒下去风险的最大值 思路: 将s+w最大的放在下面,从上往下看 解决问题的代码: #include& ...
- poj 2139 奶牛拍电影问题 floyd算法
题意:奶牛拍一系列电影,n头牛拍m部电影,同一部电影种的搭档们距离为1,求最小距离? 思路:Floyd 图 最短路径 存图: 初始化图 for (int i = 0; i <= n; i++) ...
- 洛谷 P1736 创意吃鱼法(多维DP)
题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...
- 笔记-python-lib-内置函数
笔记-python-lib-内置函数 注:文档来源为Python3.6.4官方文档 1. built-in functions abs(x) 返回绝对值 all(iterable) re ...
- java枚举类型转换为Struts2的select的数据
枚举类:AppSortEnum.java public enum AppSortEnum { CORE(0, "核心应用"), ENJOYMENT(1, "娱乐应用&qu ...