LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/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的扩展, 这里有duplicates.
时间复杂度出现了变化,原来可以根据nums[mid] 和 nums[r]的大小关系比较,判断rotate部分是在左还是在右. 依据此来判断下一步是左面查找还是右面查找,现在若是nums[l] == nums[mid]时无法判断。
Time Complexity: O(n). Space: O(1).
AC Java:
class Solution {
public int findMin(int[] nums) {
if(nums == null || nums.length == 0){
throw new IllegalArgumentException("Input array is null or empty.");
} int l = 0;
int r = nums.length-1;
while(l<r){
int mid = l+(r-l)/2;
if(nums[mid]<nums[r]){
r=mid;
}else if(nums[mid]>nums[r]){
l=mid+1;
}else{
r--;
}
} return nums[r];
}
}
LeetCode Find Minimum in Rotated Sorted Array II的更多相关文章
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- [leetcode]Find Minimum in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...
- 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. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II)
Leetcode之二分法专题-154. 寻找旋转排序数组中的最小值 II(Find Minimum in Rotated Sorted Array II) 假设按照升序排序的数组在预先未知的某个点上进 ...
- [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 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 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 ...
随机推荐
- Idea_Maven配置
操作方式:在install上右键——>Run***install 出现Run Configurations ——>右键——>Edit Run Configuration 1.Inst ...
- NSCharacterSet 简单用法
NSCharacterSet 简单用法 NSCharacterSet其实是许多字符或者数字或者符号的组合,在网络处理的时候会用到 NSMutableCharacterSet *base = [NSMu ...
- C# - MVC
Controllers 位置:Controllers 命名:HomeController // Views/Home/Index public ActionResult Index() { Vie ...
- hudson安装
Hudson 只是一个持续集成服务器(持续集成工具),要想搭建一套完整的持续集成管理平台, SVN.Maven.Sonar 等工具按需求整合则可. 1. 安装 JDK 并配置环境变量(略) J ...
- linux redis迁移
在原服务器上的redis执行save命令后,生成了dump文件,拷贝到新服务器的安装目录,可能是版本问题,数据无法还原. 针对这个问题, 1.在配置文件中加入: slave of 原服务器的ip和端口 ...
- JDBC链接数据库版本三,使用C3P0,使用jar文件两个
JdbcUtil类: package com.xiaohui.jdbc.util; import java.sql.Connection; import java.sql.PreparedStatem ...
- Grand Theft Auto V 图形研究(2)
原文链接 http://www.adriancourreges.com/blog/2015/11/02/gta-v-graphics-study-part-2/ Level of Detail 如 ...
- vFloppy1.5-虚拟启动软盘
vFloppy1.5为纯绿色免费软件,安装后不需要引导盘即可进入DOS,支持图形化访问NFTS系统格式,还可添加由光盘启动菜单选项,对于没有光驱,软驱的朋友非常实用. 到BIOS设置由光盘启动,或者打 ...
- nignx的proxy_cache
http段配置: #cache configclient_body_buffer_size 512k;proxy_connect_timeout 5;proxy_read_timeout 60;pro ...
- linux下svn的co如何排除目录
某些原因想在svn co的时候排除某些目录,可以绕个圈子,分三步来完成: co外层目录: svn checkout --depth empty $URL [$LOCATION] 完成之后,会有一个只包 ...