标签:complexity contain minimum affect become
Total Accepted: 25678 Total Submissions: 80978My Submissions
Question Solution
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.
Show Tags
分析:如果没有重复的可以直接比较就可以中间项和末尾项即可区别出使用哪一个片段,此题说明是重复项存在,因此除非极端情况下可,二分取其一处理外,其他情况两部分都要处理
例如,一种情况
3 3 3 3 3 1 3
public class Solution {
int fm(int[] nums, int start, int end){
int l=end-start+1;
if(l==1)
return nums[start];
else {
int s=start;
int e=end;
int m=(s+e)/2;
int min=0;
if(nums[m]<nums[e])
min=fm(nums, s, m);
else
{
int m1=fm(nums, s, m);
min=fm(nums, m+1, e);
if(m1<min)
min=m1;
}
return min;
}
}
public int findMin(int[] nums) {
int x=fm(nums,0,nums.length-1);
return x;
}
}
Leetcode#154Find Minimum in Rotated Sorted Array II
标签:complexity contain minimum affect become
原文地址:http://7061299.blog.51cto.com/7051299/1653326