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. linux下怎么修改grub.cfg

    一.grub2的启动配置文件grub.cfggrub2的启动配置文件grub.cfg是/boot/grub/grub.cfg,而不是以前的memu.lst.如果你是多系统,有Ubuntu和window ...

  2. 批量更新 A表的PK_ID字段

    UPDATE  ASET PK_ID=(SELECT ID FROM  B WHERE A.TAB_NAME=B.TAB_NAME AND B.IS_KEY='1' ) AB表 以TAB_NAME 做 ...

  3. BZOJ 2458 最小三角形 | 平面分治

    BZOJ 2458 最小三角形 题面 一个平面上有很多点,求他们中的点组成的周长最小的三角形的周长. 题解 跟平面最近点对差不多,也是先把区间内的点按x坐标从中间分开,递归处理,然后再处理横跨中线的三 ...

  4. 【bzoj3570】 Cqoi2014—通配符匹配

    http://www.lydsy.com/JudgeOnline/problem.php?id=3507 (题目链接) 题意 给出一个主串,里面有些通配符,'*'可以代替任意字符串或者消失,'?'可以 ...

  5. Android L开发指南

    导语:Android下一代操作系统“ L”对开发者意味着什么?ART模式能否让应用的体验超越苹果? 刚刚结束的 Google I/O大会上,Android下一代操作系统“ L”带来不少惊喜.新系统运行 ...

  6. eclipse 安装 activiti-designer-5.18.0,亲测成功

    转: eclipse 安装 activiti-designer-5.18.0,亲测成功 2018年06月02日 15:50:05 ldw4033 阅读数:2826   版权声明:本文为博主原创文章,未 ...

  7. 野指针(Wild pointer)和悬垂指针(dangling pointer)

    详细参考如下: Dangling pointer(悬垂指针.迷途指针)和 Wild pointer(野指针) 迷途指针经常出现在混杂使用malloc() 和 free() 库调用: 当指针指向的内存释 ...

  8. 《编程快速上手》--web抓取--利用webbrowser模块的mapIT.py

    1.代码如下 #! python3 # mapIT.py - Launches a map in the browser using an address from the # command lin ...

  9. 前端案例分享(一):CSS+JS实现流星雨动画

    目录 引言 1.效果图 2.源码 3.案例解析 4.小问题 5.结语 引言        平常会做一些有意思的小案例练手,通常都会发到codepen上,但是codepen不能写分析.        所 ...

  10. opencv 图像处理函数大全

    .cvLoadImage:将图像文件加载至内存: .cvNamedWindow:在屏幕上创建一个窗口: .cvShowImage:在一个已创建好的窗口中显示图像: .cvWaitKey:使程序暂停,等 ...