标签:ase ber find return public int etc cas .com
找出一個大於兩邊的頂點,可以把nums[-1] ,nums[n] 都視為負無限
因為我用 int.minvalue來代表 num[-1] 及 num[n]的值
所以leetcode的test case 有一個測試是 [-2147483648] ,用來坑minvalue的
public class Solution { public int FindPeakElement(int[] nums) { int leftNeighber = int.MinValue; int rightNeighber = int.MinValue; int current = int.MinValue; int result = -1; for(int i = 0; i < nums.Length; i++) { current = nums[i]; if(i -1 >=0 && i -1 <= nums.Length - 1) { leftNeighber = nums[i - 1]; } else { leftNeighber = int.MinValue; } if (i + 1 >= 0 && i + 1 <= nums.Length - 1) { leftNeighber = nums[i + 1]; } else { rightNeighber = int.MinValue; } if ((leftNeighber == int.MinValue || current > leftNeighber) && (rightNeighber == int.MinValue || current > rightNeighber)) { result = i; break; } } return result; } }
标签:ase ber find return public int etc cas .com
原文地址:https://www.cnblogs.com/seako/p/10891377.html