publicclass{ * @param nums: a mountain sequence which increase firstly and then decrease * @return: then mountain top */ publicintmountainSequence(int[] nums){ // write your code here if (nums == null || nums.length == 0) { return0; }
int start = 0, end = nums.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2; if (nums[mid] > nums[mid - 1]) { // target on the right(include mid) start = mid; } elseif (nums[mid] < nums[mid - 1]) { // target on the left(exclude mid) end = mid - 1; } else { end = mid;//start = mid; 不会出现这种情况,in this case, can't do binary search } } if (nums[start] > nums[end]) { return nums[start]; } else { return nums[end]; }
近期评论