publicintminArray(int[] numbers){ int left = 0; int right = numbers.length-1; while(left < right && (numbers[right-1] == numbers[right])){ right = right-1; } while(left < right && (numbers[left+1] == numbers[left])){ left = left+1; } while (left < right){ int mid = left + (right - left) /2; if (numbers[left] < numbers[right]){ return numbers[left]; } if (numbers[mid] < numbers[left] ){ right = mid; while(left < right && (numbers[right-1] == numbers[right])){ right = right-1; } } elseif (numbers[mid] >= numbers[left]){ left = mid + 1; while(left < right && (numbers[left+1] == numbers[left])){ left = left+1; } } } return numbers[left]; }
补救代码二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicintminArray(int[] numbers){ int left = 0; int right = numbers.length-1; while (left < right){ int mid = left + (right - left) /2; if (numbers[left] < numbers[right]){ return numbers[left]; } if (numbers[mid] < numbers[left] ){ right = mid; } elseif (numbers[mid] >= numbers[left]){ left = mid + 1; } else{ left += 1; } } return numbers[left]; }