https://oj.leetcode.com/problemset/algorithms/
Http://siddontang.gitbooks.io/leetcode-solution/content/array/find_peak_element.html
Public class solution { public int findpeakelement (int[] NUM) { // solution a: return findpeakelement_binary (num); // Solution b: // return findpeakelement_gready (num ); } ///////////////////// // solution a: binary // public int Findpeakelement_binary (Int[] num) { int low = 0; int high = num.length - 1; while (Low <= high) { int mid = low + (High - low) / 2; if (mid == 0 | | &NBSP;NUM[MID]&NBSP;>=&NBSP;NUM[MID&NBSP;-&NBSP;1]) && (mid == num.length - 1 | | &NBSP;NUM[MID]&NBSP;>=&NBSP;NUM[MID&NBSP;+&NBSP;1]) { return mid; } else if (mid > 0 && Num[mid - 1] >= num[mid]) { high = mid - 1; } else { low = mid + 1; } } return num[low]; } ///////////////////// // solution a: gready // Public int findpeakelement_gready (Int[] num) { // Iterate every elements, if it is greater than its neighbours, return the value. if (num == null | | num.length == 0) return -1; // The input array cannot be null if (num.length == 1) return 0; // A edge case. for (int i = 0 ; i < num.length ; i ++) { int cur = num[i]; int pre = i == 0 ? integer.min_value : num[i - 1]; int next = i == num.length - 1 ? Integer.MIN_VALUE : num[i + 1]; if (cur > pre && cur > next) { return i; } } // no i did ' t find it. // it is possible that all numbers are the same return -1; }}
[leetcode]162 Find Peak Element