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的更多相关文章

  1. 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 ...

  2. Leetcode153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中最小值

    假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重 ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

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

  4. [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 ...

  5. 【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 ...

  6. 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 ...

  7. LeetCode Find Minimum in Rotated Sorted Array II

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...

  8. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  9. Find Minimum in Rotated Sorted Array II

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

随机推荐

  1. BinaryReader 和BinaryWriter 读写类对象

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  2. C++ 11 笔记 (二) : for循环

    首先肯定的是,我不是标题党.. C++11的for循环确实有跟C++98不一样的地方,还是先上代码: , , , , }; for (int x : test_arr) { std::cout < ...

  3. linux线程(二)内存释放

    linux线程有两种模式joinable和unjoinable. joinable线程:系统会保存线程资源(栈.ID.退出状态等)直到线程退出并且被其他线程join. unjoinable线程:系统会 ...

  4. Standford CoreNLP--Sentiment Analysis初探

    Stanford CoreNLP功能之一是Sentiment Analysis(情感分析),可以标识出语句的正面或者负面情绪,包括:Positive,Neutral,Negative三个值. 运行有两 ...

  5. 编译Firebird的源码

    编译步骤:一.下载所需的软件  1.下载FB2.0 RC4       http://optusnet.dl.sourceforge.net/sourceforge/firebird/Firebird ...

  6. haskell Types 和 Typeclasses

    Algebraic Data Types 入门 在前面的章节中,我们谈了一些 Haskell 内置的类型和 Typeclass.而在本章中,我们将学习构造类型和 Typeclass 的方法. 我们已经 ...

  7. 【HDOJ】1242 Rescue

    BFS+优先级队列. #include <iostream> #include <cstdio> #include <cstring> #include <q ...

  8. 【转】Android4.4 之Bluetooth整理

    原文网址:http://www.cnblogs.com/shed/p/3737016.html Android 4.4上蓝牙协议栈采用的是BRCM和Google共同开发的bluedroid,代替了之前 ...

  9. Bootstrap框架中的字形图标的理解

    最近项目中准备使用 Bootstrap 框架,看中了Ace Admin 这套皮肤,看其代码的时候,发现使用了字形图标.下面内容来源于网络,根据自己对新知识的学习曲线重新整合了一下: 一,字形图标的定义 ...

  10. ASPNETMVC多语言方案

    ASPNETMVC多语言方案 前言: 好多年没写文章了,工作很忙,天天加班, 每天都相信不用多久,就会升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰,想想还有点小激动~~~~ 直到后来发生 ...