Find Minimum in Rotated Sorted Array I

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.

思路:

当nums[left]<nums[right],说明数组没有旋转,是按升序排列的。可直接返回nums[left];

当nums[left]<nums[mid],说明left至mid这段是按升序排列的,可令left=mid+1;

当nums[left]>nums[mid],说明mid至right这段是按升序排列的,可令right=mid;

理清思路后,代码就变的异常简单了,下面的代码不是按照这个思路来的,这个思路是写II的时候才想出来的,用这个思路来写的话,非常好理解。

class Solution {
public:
int findMin(vector<int> &num) {
int len=num.size();
int Left,Right,Mid;
Left=;
Right=len-;
while(Left<=Right)
{
Mid=Left+(Right-Left)/;
if(num[len-]<num[Mid])
Left=Mid+;
else
Right=Mid-;
}
return num[Left];
}
};

Find Minimum in Rotated Sorted Array II

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

Would this affect the run-time complexity? How and why?

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.

The array may contain duplicates.

这个思路和1差不多,关键是怎么处理重复的元素。

代码中continue那个语句是亮点。

class Solution {
public:
int findMin(vector<int>& nums) {
int len=nums.size();
int left=;
int right=len-;
int mid=;
while(left<right)
{
if(nums[left]==nums[right])
{
left++;
continue;
}
if(nums[left]<nums[right]) //这一步其实才是最大的亮点啊,解决了left=mid+1所带来的困惑,而且更加的高效
return nums[left];
mid=(left+right)/;
if(nums[mid]>=nums[left])
left=mid+;
else
right=mid;
}
return nums[left];
}
};

  

Find Minimum in Rotated Sorted Array I&&II——二分查找的变形的更多相关文章

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

  2. Find Minimum in Rotated Sorted Array I & II

    Find Minimum in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to yo ...

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

  4. 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)

    1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...

  5. [OJ] Find Minimum in Rotated Sorted Array II

    LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...

  6. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  7. 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  8. LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...

  9. Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)

    Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...

随机推荐

  1. Linux安装cx_Oracle

    安装依赖包 yum -y install gcc python-devel 确认以下变量都已经设置好了 export ORACLE_HOME=/home/oracle/app/oracle/produ ...

  2. Codeforces Round #345 (Div. 2) B

    B. Beautiful Paintings time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. Caused by: java.io.IOException: Filesystem closed的处理

    org.apache.hadoop.hive.ql.metadata.HiveException: Unable to rename output from: hdfs://nameservice/u ...

  4. 第三方库升级Nginx

    通过PPA方式,来升级Nginx 1. 添加PPA sudo add-apt-repository ppa:nginx/stable sudo apt-get updatesudo apt-get u ...

  5. [技巧篇]08.Struts2拦截器中获取Servlet API方法

    讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找

  6. LightOJ 1097 - Lucky Number 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3 ...

  7. Send Email in Robot Framework Python Using Gmail

    转载自:http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-fra ...

  8. modelsim10 SE 仿真lattice Xp2工程

    1.首先要建立Lattice XP2库 在modelsim10 SE启动后.首先指定Lattice Diamond 1.4 给定的仿真器库源代码编译目录: C:\lscc\diamond\1.4\ca ...

  9. C++ Vector 中自定义对象的排序

    需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...

  10. js_同步和异步

    刚开始写js那会,对这一块是知之甚少,太多太多的知识不足,致使做什么都很艰难.现在工作也有段时间了,知识也有了点积累, 写点什么分享一下. 同步和异步?这个问题是在使用ajax请求后台数据的时候出现的 ...