首页 > 代码库 > Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode
Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode
递归
public class Solution { public int findMin(int[] num) { return helper(num, 0, num.length-1); } //with duplicate public static int helper(int[] a, int left, int right){ //one element if(left == right){ return a[left]; } //two elements if(left == right-1){ return a[left]<a[right]? a[left]: a[right]; } //the array is ordered if(a[left] < a[right]){ return a[left]; } int mid = (left+right)/2; if(a[mid] > a[left]){ return helper(a, mid, right); }else if(a[mid] < a[left]){ return helper(a, left, mid); }else{ //when a[mid] == a[left], we have to search both side return Math.min(helper(a, mid, right), helper(a, left, mid)); } } }
Find Minimum in Rotated Sorted Array II 旋转数组中找最小值(有重复元素) @LeetCode
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。