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 ...
随机推荐
- GridControl/GridView的分组操作
今天在模块编写中碰到了对表格的分组,特意在这里把它记录下来. 一.背景:Dev14.1.3,GridControl,.NET4.0+C# 二.过程 1.GridControl设计 一共添加4列:在下面 ...
- 学习smail注入遇到的坑
1.将需要被反编译的apk包解开之后,找到MainActivity,然后在OnCreate中添加需要加入注入的smail代码: Java代码: /** * 获取Android id * * @para ...
- CentOS 7将Python 2.X.X升级到Python 3.X.X
# cd /usr/local/src/ # ls Python-.tgz # .tgz # ls Python- Python-.tgz # cd Python-/ # yum install gc ...
- background-size (设置背景图片的大小)
设置背景图片的大小,以长度值或百分比显示(数值包括 长度length和百分比percentage),还可以通过cover和contain来对图片进行伸缩. 语法:background-size: au ...
- PHP字符串转义
与PHP字符串转义相关的配置和函数如下: 1.magic_quotes_runtime 2.magic_quotes_gpc 3.addslashes()和stripslashes() 4.mysql ...
- layout布局
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【IE6的疯狂之二】IE6中PNG Alpha透明(全集)
ie7,fireofx,opera,及至webkit内核的chrome ,safari….. 这些浏览器均支持png的Alpha透明. 很多人说IE6不支持PNG透明,其实IE支持100%透明的PNG ...
- AppDelegate 里一个基本的跳转方法,用来在rootView崩溃的时候直接调试我自己的页面
将 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lau ...
- Qt对xml文件的读写
最近研究了一下qt下对xml文件的读写,小计一下,成为自己的知识. main函数调用: #include <QApplication> #include "readconfig. ...
- tfs 清除缓存,在需要时
C:\Users\xxx\AppData\Local\Microsoft\Team Foundation\5.0