标签:写法 src .com blog 倒数 无法 条件 param except
Given a mountain sequence of n
integers which increase firstly and then decrease, find the mountain top.
Example
Given nums
= [1, 2, 4, 8, 6, 3]
return 8
Given nums
= [10, 9, 8, 7]
, return 10
public class Solution { /* * @param nums: a mountain sequence which increase firstly and then decrease * @return: then mountain top */ public int mountainSequence(int[] nums) { // 要怎么处理输入??? if (nums == null || nums.length == 0){ throw new IllegalArgumentException(); } int start = 0; int end = nums.length - 1; while (start + 1 < end){ int mid = start + (end - start) / 2; // 应该不用判断mid < nums.length - 1把,天然的。 // 等于情况似乎无法处理,输入应该这种描述不会给吧 if (nums[mid + 1] - nums[mid] > 0){ start = mid; } else { end = mid; } } if (nums[start] > nums[end]){ return nums[start]; } return nums[end]; } }
lintcode585- Maximum Number in Mountain Sequence- medium
标签:写法 src .com blog 倒数 无法 条件 param except
原文地址:http://www.cnblogs.com/jasminemzy/p/7580047.html