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 ...
随机推荐
- 坑爹的NSIS转义符:$ (在NSIS的MessageBox中写换行文字)
最近的项目中,发现了NSIS一个比较坑的地方:$ 不但是变量常量的开头,还是一个转义字符. 大家有没有发现,NSIS写的脚本中,如果要让弹出消息框中的文字带换行功能,“\r\n” 是不是很不管用呢? ...
- 通过本地加载ga.js文件提高Google Anlytics性能
Google Anlytics 分析代码是异步加载的,一般来讲不会影响网页性能,但是技术部的网页性能报告里老是提到ga.js的状态为Aborted,说明ga虽然是异步跟踪,但某些情况下对网页性能与加载 ...
- Java语言基础(六)char成员变量默认初始值 最简单的Java源文件 Java的main()方法
①char成员变量的初始值是:'\u0000' ②package用来指定该文件所处的包的名称,必须位于源文件的顶端. import java.util.*; package com.hyy.test; ...
- Winform的多线程问题
http://blog.csdn.net/Maths_bai/article/details/6000744
- 【Spring】Spring IOC原理及源码解析之scope=request、session
一.容器 1. 容器 抛出一个议点:BeanFactory是IOC容器,而ApplicationContex则是Spring容器. 什么是容器?Collection和Container这两个单词都有存 ...
- 为什么手机连接wifi会显示已停用?
1.通常导致手机连接WiFi显示“已停用”故障的原因是由于无线路由器“安全模式”设置不当造成的,对此我们可以通过以下方法来解决: 2.根据无线路由器背面的信息(包括路由器IP地址,登陆用户名和密码), ...
- java常量使用比较好的方法
1.首先建立一个工具类 public class AppConst { private static Map<String,String> map=new HashMap<Strin ...
- Android推送通知Notification
参考: 1.http://www.cnblogs.com/anrainie/archive/2012/03/07/2383941.html 总结: 代码:
- Android源码之Matrix
Matrix类在Android中主要用来进行矩阵变换,其可操作的对象包括图像.点阵.Vector(向量).矩形等. Matrix支持的变换类型主要有以下几种: 1.Translate:平移变换 2.R ...
- CF 370
A:http://codeforces.com/problemset/problem/370/A #include<stdio.h> #include<string.h> #i ...