Leetcode / 162. Find Peak Element
Pick a programming language:
Here is the source code for the solution to this problem.
impl Solution {
pub fn find_peak_element(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = left + ((right - left) >> 1);
if nums[mid] > nums[mid + 1] {
right = mid;
}
else {
left = mid + 1;
}
}
return left as i32;
}
}
class Solution {
public int findPeakElement(int[] nums) {
int left = 0;
int right = nums.length - 1;
while (left < right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] > nums[mid + 1]) {
right = mid;
}
else {
left = mid + 1;
}
}
return left;
}
}
Did you like the lesson? 😆👍
Consider a donation to support our work: