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 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.
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.
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的更多相关文章
- 【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 ...
- 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 ...
- 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 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 ...
- [OJ] Find Minimum in Rotated Sorted Array II
LintCode 160. Find Minimum in Rotated Sorted Array II (Medium) LeetCode 154. Find Minimum in Rotated ...
- 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 ...
- 【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 ...
- 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 ...
- Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
随机推荐
- servletActionContext.getContext默认获取contextmap 数据默认存储在contextmap的request中
- CentOS 7 上安装(LAMP)服务 Linux,Apache,MySQL,PHP
介绍 LAMP 是现在非常流行的 WEB 环境, 是 Linux,Apache,MySQL,PHP 的缩写.数据存储在 MySQL 中,动态内容由 PHP 处理. 在本指南中,我们将演示如何在 Cen ...
- ansible部署(pip安装)
centos7 pip安装 ansible 首先ansible基于python2.X 环境 默认centos都已经安装好了python2环境 安装可选性 ansible可以通过源码,yum,pip等方 ...
- 【刷题】BZOJ 3531 [Sdoi2014]旅行
Description S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰 ...
- 聊聊flink的NetworkEnvironmentConfiguration
本文主要研究一下flink的NetworkEnvironmentConfiguration NetworkEnvironmentConfiguration flink-1.7.2/flink-runt ...
- 安装GourdScanV2的踩坑过程
环境:ubuntu 16.04.1 1.安装dcoker sudo apt-get install docker.io 坑:sudo apt-get install docker 2.下载关于dock ...
- 使用mshta.exe绕过应用程序白名单(多种方法)
0x00 简介 很长一段时间以来,HTA文件一直被web攻击或在野恶意软件下载程序用作恶意程序的一部分.HTA文件在网络安全领域内广为人知,从红队和蓝队的角度来看,它是绕过应用程序白名单有价值的“ ...
- 配置nginx为FastDFS的storage server提供http访问接口
1.拉取模块代码 # git clone https://github.com/happyfish100/fastdfs-nginx-module.git 2.编译安装nginx,添加支持fastdf ...
- Android自定义 Dialog 对话框
Android自定义Dialoghttp://www.cnblogs.com/and_he/archive/2011/09/16/2178716.html Android使用自定义AlertDialo ...
- C#线程篇---线程池如何管理线程(6完结篇)
C#线程基础在前几篇博文中都介绍了,现在最后来挖掘一下线程池的管理机制,也算为这个线程基础做个完结. 我们现在都知道了,线程池线程分为工作者线程和I/O线程,他们是怎么管理的? 对于Microsoft ...