本文主要是介绍leetcode解题方案--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?
分析
题目改成了可能会重复,经典测试例是【1,1,1,10,10】
所以要考虑相等的情况。如果遇到相等,就–
class Solution {public static int findMin(int[] nums) {int low = 0;int high = nums.length-1;if (nums[high] > nums[low]) {return nums[low];}while (high - low >1) {int mid = (high + low)/2;if (nums[mid] < nums[high]) {high = mid;} else if (nums[mid] > nums[high]){low = mid;} else {high--;}}return nums[high];}
}
这篇关于leetcode解题方案--154--Find Minimum in Rotated Sorted Array II的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!