首页 > 代码库 > [LintCode] 585 Maximum Number in Mountain Sequence

[LintCode] 585 Maximum Number in Mountain Sequence

4/23/2017

算法班

注意返回的是值,而不是index

 1     public int mountainSequence(int[] nums) {
 2         if (nums == null || nums.length == 0) return -1;
 3         int start = 0, end = nums.length - 1;
 4         int ret;
 5         
 6         while (start + 1 < end) {
 7             int mid = start + (end - start) / 2;
 8             
 9             if (nums[mid] > nums[mid - 1] && nums[mid] < nums[mid + 1]) return nums[mid];
10             if (nums[mid] > nums[mid - 1] && nums[mid] > nums[mid + 1]) {
11                 end = mid;
12             } else {
13                 start = mid;
14             }
15         }
16         if (nums[start] < nums[end]) return nums[end];
17         return nums[start];
18     }

 

[LintCode] 585 Maximum Number in Mountain Sequence