LeetCode153: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.
不知道这道题为什么难度是Medium,感觉蛮简单的。
仅仅须要找到第一个大于它后面的数,它后面的数就是旋转排序数组中最小的数。
将返回结果初始化为数组中的第一个元素。这样就能够将结果统一起来。
时间复杂程度是O(N)。
runtime:4ms
class Solution {
public:
int findMin(vector<int>& nums) {
int result=nums[0];
auto iter=nums.begin();
for(;iter!=nums.end()-1;iter++)
{
if(*iter>*(iter+1))
{
result=*(iter+1);
break;
}
}
return result;
}
};
然后看了下Discuss,发现了一个使用二分查找思想的代码。漫有意思的,也有分析。以后在碰到排序好的数组进行了一些变形或一些附加说明时注意使用二分查找的思想。时间复杂程度是O(logN)。
In this problem, we have only three cases.
Case 1. The leftmost value is less than the rightmost value in the list: This means that the list is not rotated. e.g> [1 2 3 4 5 6 7 ]
Case 2. The value in the middle of the list is greater than the leftmost and rightmost values in the list. e.g> [ 4 5 6 7 0 1 2 3 ]
Case 3. The value in the middle of the list is less than the leftmost and rightmost values in the list. e.g> [ 5 6 7 0 1 2 3 4 ]
As you see in the examples above, if we have case 1, we just return the leftmost value in the list. If we have case 2, we just move to the right side of the list. If we have case 3 we need to move to the left side of the list.
Following is the code that implements the concept described above.
int findMin(vector<int>& nums) {
int left = 0, right = nums.size() - 1;
while(left < right) {
if(nums[left] < nums[right])
return nums[left];
int mid = (left + right)/2;
if(nums[mid] > nums[right])
left = mid + 1;
else
right = mid;
}
return nums[left];
}
LeetCode153:Find Minimum in Rotated Sorted Array的更多相关文章
- LeetCode153 Find Minimum in Rotated Sorted Array. LeetCode162 Find Peak Element
二分法相关 153. Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unkn ...
- Leetcode153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中最小值
假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...
- [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 I&&II
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
- 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
原题链接在这里: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 就是找到第一个违反升序的值,就 ...
- Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- ListToDataTable
public static DataTable ToDataTable<T>(IEnumerable<T> collection) { var ...
- 加密算法 - RSA
与DES不同,RSA算法中,每个通信主体都有两个钥匙,一个公钥一个私钥. 就是有2把钥匙1.使用publicKey可以对数据进行加密2.使用Key才能对数据进行解密单方向传输用公钥加密的数据,只有私钥 ...
- YUV数据格式
概要: 与RGB编码方法类似,YUV也是一种颜色编码方法,主要用于电视系统以及模拟视频领域,它是指将亮度参量(Y:Luminance或Luma)和色度参量(UV:Chrominance或Chroma) ...
- MySQL查询原理及其慢查询优化案例分享(转)
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更 好的使用它,已经成为开发工程师的必修课,我们经常会从职 ...
- Android Density(密度)
1. 什么是density 引用 1) density density表示每英寸有多少个显示点(逻辑值),它的单位是dpi:dot per inch,通常屏幕大时,density就大,屏幕小时,de ...
- perl unload utf-8 oracle 数据库
perl unload utf-8 Oracle [oracle@oadb sbin]$ sqlplus / as sysdba SQL*Plus: Release 11.2.0.4.0 Produc ...
- [LeetCode#156] Binary Tree Upside Down
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
- ASP.NET MVC 4 RC的JS/CSS打包压缩功能 (转载)
ASP.NET MVC 4 RC的JS/CSS打包压缩功能 打包(Bundling)及压缩(Minification)指的是将多个js文件或css文件打包成单一文件并压缩的做法,如此可减少浏览器需下载 ...
- Away3d 基础 1 ---对一个简单类的解释
转自:http://www.cnblogs.com/nooon/archive/2009/05/16/1458334.html 原英文地址: http://www.flashmagazine.com/ ...
- Windows 8/8.1系统下硬盘占用率100%的问题解决思路汇总
家庭组 2.关闭虚拟内存 3.关闭Windows 8快速开机功能 4.服务进程superfetch 5.系统下软件排除 操作步骤: 1.Windows 8系统的家庭组方便多人不用存储设备就能在局域网中 ...