From : https://leetcode.com/problems/decode-ways/
A peak element is an element that is greater than its neighbors.
Given an input array where num[i] ≠ num[i+1]
, find a peak element and return
its index.
The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
You may imagine that num[-1] = num[n] = -∞
.
For example, in array [1, 2, 3, 1]
, 3 is a peak element and your function
should return the index number 2.
class Solution {
public:
int findPeakElement(vector<int>& nums) {
int n = nums.size();
if(n == 1) return 0;
if(nums[0] > nums[1]) return 0;
if(nums[n-1] > nums[n-2]) return n-1;
for(int i = 1; i < n-1; i ++)
if(nums[i] > nums[i-1] && nums[i] > nums[i+1])
return i;
return -1;
}
};
public class Solution {
public int findPeakElement(int[] nums) {
if(nums == null || nums.length == 0) {
return 0;
}
int l1 = nums.length-1, i=0;
while(i < l1) {
if(nums[i] > nums[i+1]) {
break;
}
++i;
}
return i;
}
}
public class Solution {
public int findPeakElement(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
return find(nums, 0, nums.length - 1);
}
private int find(int[] nums, int low, int high) {
if (low == high) {
return low;
}
int mid = (high + low) >> 1;
if (nums[mid] > nums[mid + 1]) {
return find(nums, low, mid);
} else {
return find(nums, mid + 1, high);
}
}
}