[LintCode] 585 Maximum Number in Mountain Sequence

Posted panini

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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的主要内容,如果未能解决你的问题,请参考以下文章

LintCode: Maximum Depth of Binary Tree

lintcode-medium-Maximum Subarray II

lintcode-easy-Maximum Depth of Binary Tree

[LintCode] Maximum Subarray 最大子数组

[LintCode] Maximum Gap 求最大间距

lintcode-medium-Binary Tree Maximum Path Sum