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.

Notice

You may assume no duplicate exists in the array.

Example

Given [4, 5, 6, 7, 0, 1, 2] return 0

分析:

Although array is rotated, either its left half part or right half part is sorted. We just need to disgard the sorted part, and continue to search in the rotated part. Unlike regular binary search in which start = mid + 1 or end = mid - 1, we need to set start = mid or end = mid, this is to make sure the remaining half is still rotated. When there are only two numbers left, the right one must be the smallest value in the whole rotated array.

 public class Solution {
public int findMin(int[] num) {
if (num == null || num.length == ) return -;
if (num.length == ) return num[];
int start = , end = num.length - ;
while (start <= end) {
if (num[start] <= num[end]) return num[start];
if (start + == end) return num[end]; int mid = start + (end - start) / ;
if (num[start] < num[mid]){
start = mid + ;
} else {
end = mid;
}
}
return ;
}
}

递归的方法:

 class Solution {
public int findMin(int[] nums) {
if (nums == null || nums.length == ) return -;
return findMinHelper(nums, , nums.length - );
} private int findMinHelper(int[] nums, int start, int end) {
if (start == end) return nums[start];
if (end - start == ) return Math.min(nums[start], nums[end]);
if (nums[start] < nums[end]) {
return nums[start];
}
int mid = start + (end - start) / 2;
if (nums[start] < nums[mid]) {
return findMinHelper(nums, mid + , end);
} else {
// [3, 1, 2]
return findMinHelper(nums, start, mid);
}
}
}

Find Minimum in Rotated Sorted Array II

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.

Notice

The array may contain duplicates.

Example

Given [4,4,5,6,7,0,1,2] return 0.

Analysis:

In this case, we have duplicates in the array, so, we cannot compare arr[start] and arr[mid] to determine which part is the rotated part. In stead, we have check all the numbers in the left part.

 public class Solution {

     public int findMin(int[] num) {
if (num == null || num.length == ) return -;
if (num.length == ) return num[]; int start = ;
int end = num.length - ; if (num[start] < num[end]) return num[start]; // handle case 4 5 6 7 0 1 2
while (start <= end) {
if (start + == end) return num[end]; // eventually, there will be only two numbers left
int mid = start + (end - start) / ;
if (nonDecreasing(num, start, mid)){
start = mid;
} else {
end = mid;
}
}
return ;
} private boolean nonDecreasing(int[] A, int i, int j) {
for (int k = i; k < j; k++) {
if (A[k] > A[k + ] ) return false;
}
return true;
}
}

递归解法:

 class Solution {
public int findMin(int[] nums) {
if (nums == null || nums.length == ) return -;
return findMinHelper(nums, , nums.length - );
} private int findMinHelper(int[] nums, int start, int end) {
if (start == end) return nums[start];
if (end - start == ) return Math.min(nums[start], nums[end]);
int mid = start + (end - start) / ;
boolean firstHalfSorted = sorted(nums, start, mid);
boolean secondHalfSorted = sorted(nums, mid, end); if (firstHalfSorted && secondHalfSorted) {
return nums[start];
} if (firstHalfSorted) {
return findMinHelper(nums, mid + , end);
} else {
return findMinHelper(nums, start, mid);
}
} private boolean sorted(int[] A, int i, int j) {
for (int k = i; k < j; k++) {
if (A[k] > A[k + ] ) return false;
}
return true;
}
}

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. line search中的重要定理 - 梯度与方向的点积为零

    转载请注明出处:http://www.codelast.com/ 对精确的line search(线搜索),有一个重要的定理: ∇f(xk+αkdk)Tdk=0 这个定理表明,当前点在dk方向上移动到 ...

  2. 题解 CF1005A 【Tanya and Stairways】

    楼上别说这个题水,这个题可能还真有不知道的知识点. 看到这个题,想到刚学的单调栈. 单调栈? 单调栈和单调队列差不多,但是我们只用到它的栈顶. 单调,意思就是一直递增或者递减. 这跟这个题有什么关系? ...

  3. mysql查看表中列信息

    查看所有数据库中所有表的数据库名和表名 SELECT `TABLES`.`TABLE_SCHEMA`, `TABLES`.`TABLE_NAME` FROM `information_schema`. ...

  4. Fair CodeForces - 987D(巧妙bfs)

    题意: 有n个城市 m条边,每条边的权值为1,每个城市生产一种商品(可以相同,一共k种),求出分别从每个城市出发获得s种商品时所走过路的最小权值 解析: 我们倒过来想,不用城市找商品,而是商品找城市, ...

  5. 【转】iBatis.Net的SqlMap.config文件

    转自:http://www.xuebuyuan.com/579671.html   iBatis.Net基本的运行环境配置主要由两个文件组成,分别是SqlMap.config和Provider.con ...

  6. MT【141】逆用特征根法

    (清华大学THUSSAT) 已知 \(a=\left( \dfrac{-1+\sqrt{5}}{2} \right)^{-10}+\left( \dfrac{-1-\sqrt{5}}{2} \righ ...

  7. Navicat使用教程:获取MySQL中的行数(第1部分)

    下载Navicat Premium最新版本 Navicat Premium是一个可连接多种数据库的管理工具,它可以让你以单一程序同时连接到MySQL.Oracle及PostgreSQL数据库,让管理不 ...

  8. BZOJ 2668: [cqoi2012]交换棋子

    2668: [cqoi2012]交换棋子 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1112  Solved: 409[Submit][Status ...

  9. fzyzojP2291 -- 小添添的庄园之道路修复

    直接换根dp f[i]表示,i为根的子树的方案 f[i]=Π(f[son]+1)(就是考虑这个边修不修(不修,子树中只有一种方案)) 这里是乘法 换根的时候,直接算的话,为了消除x对fa的贡献,要乘上 ...

  10. [收藏转载链接]Opencv部分

    转载自-柳如风-http://www.cnblogs.com/rongfangliu/p/opencvlink.html [收藏夹整理]OpenCV部分   OpenCV中文论坛 OpenCV论坛 o ...