My code is wrong, but is accepted [Find Minimum in Rotated Sorted Array II]


  • 0
    F

    my code is accepted, but I find a wrong case { 10, 1, 10, 10, 10,10,10,10,10 }, my code is

    static int findMin(int[] num) {
    	return findMin(num, 0, num.length - 1);
    }
    
    static int findMin(int[] num, int start, int end) {
    	if (start == end)
    		return num[start];
    
    	if (start + 1 == end)
    		return Math.min(num[start], num[end]);
    
    	int mid = start + (end - start) / 2;
    	if (num[mid] == num[start] && num[mid] == num[mid - 1])
    		return findMin(num, mid, end);
    	if (num[mid] == num[end] && num[mid] == num[mid + 1])
    		return findMin(num, start, mid);
    	if (num[mid] >= num[start] && num[mid] > num[end])
    		return findMin(num, mid + 1, end);
    	if (num[mid] < num[start] && num[mid] <= num[end])
    		return findMin(num, start, mid);
    	return num[start];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.