LeetCode OJ 154. 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.
【题目分析】
与Find Minimum in Rotated Sorted Array题目相比,这个题目中数组元素可能是重复的。
【思路】
【java代码】非递归
public class Solution {
public int findMin(int[] nums) {
int start=0,mid=0,end=nums.length-1;
while(start<end){
if(nums[start] < nums[end]) return nums[start];
mid=start+(end-start)/2;
if(nums[mid]>nums[end]) start=mid+1;
else if(nums[mid]==nums[end]) end--;
else end=mid;
}
return nums[start];
}
}
public class Solution {
public int findMin(int[] nums) {
int len = nums.length;
if(len == 1) return nums[0];
return find(nums, 0, len-1);
}
public int find(int nums[], int left, int right){
while(left < right){
if(nums[left] < nums[right]) return nums[left];
if(left == right - 1) return Math.min(nums[left], nums[right]);
int mid = left + (right - left)/2;
if(nums[left] == nums[mid] && nums[mid] == nums[right])
return Math.min(find(nums, left, mid-1), find(nums, mid+1, right));
if(nums[left] <= nums[mid]) left = mid + 1;
else right = mid;
}
return nums[left];
}
}
LeetCode OJ 154. Find Minimum in Rotated Sorted Array II的更多相关文章
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【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】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- 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 新题: 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. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i. ...
- Java for LeetCode 154 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 migh ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- leetcode 154. Find Minimum in Rotated Sorted Array II --------- java
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- Freeplane中的自动边线颜色功能
今天我将电脑上的Freeplane从1.3.11升级到了1.5.18.发现新版本已经没有了1.3.11中的菜单选项Format → “Automatic edge color”.搜索了一下才发现,该功 ...
- 安卓平台 全面支持软解和硬解的SDK-Demo源代码开放
专业做视频编解码的SDK开发工作. 2015年12月1日10:46:55: 更新到1.5.0版本 功能列表: 基本播放: 1,正常播放, 支持MP4,FLV,AVI,TS,3GP,RMVB,WM,WM ...
- Linux软件安装管理 - CentOS
---恢复内容开始--- 1. 软件包管理简介 1.1 源码包 - 脚本安装包 1.2 二进制包(RPM包,系统默认包) - 依赖性 2. rpm命令管理(Redhat Package Manager ...
- linux操作数据库
1.显示数据库 show databases; 2.选择数据库 use 数据库名; 3.显示数据库中的表 show tables; 4.显示数据表的结构 describe 表名; 5.显示表中记录 S ...
- java全组合算法
public static void combination(int[] s) { if (s.length == 0) { return; } int len = s.length; int n = ...
- spring security:ajax请求的session超时处理
当前端在用ajax请求时,如果没有设置session超时时间并且做跳转到登录界面的处理,那么只是靠后台是很难完成超时的一系列动作的:但是如果后台 没有封装一个ajax请求公共类,那么在ajax请求上下 ...
- 利用flashBack恢复误删除(delete)的表数据
Flashback query(闪回查询)原理 Oracle根据undo信息,利用undo数据,类似一致性读取方法,可以把表置于一个删除前的时间点(或SCN),从而将数据找回. Flashback q ...
- poj 1308Bugs Integrated, Inc. [三进制状压]
题目链接[http://poj.org/problem?id=1038] 题意: 给出一个N*M大小的图,图中有K个坏点.N (1 <= N <= 150), M (1 <= M & ...
- java note
1.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重复.
- ubuntu 16.04下搭建web服务器(MySQL+PHP+Apache) 教程
1.开始说明 下面很多可能参照网上其中以为前辈的,但有所改进吧.这些设置可能会有所不同,你需要根据不同情况进行修改. 安装apache2 2.切换管理员身份 在ubuntu中需要用root身份进行操作 ...