一、题目
峰值元素是指其值严格大于左右相邻值的元素。
给你一个整数数组 nums,找到峰值元素并返回其索引。数组可能包含多个峰值,在这种情况下,返回 任何一个峰值 所在位置即可。
你可以假设 nums[-1] = nums[n] = -∞ 。
你必须实现时间复杂度为 O(log n) 的算法来解决此问题。
示例 1:
输入:nums = [1,2,3,1]
输出:2
解释:3 是峰值元素,你的函数应该返回其索引 2。
示例 2:
输入:nums = [1,2,1,3,5,6,4]
输出:1 或 5
解释:你的函数可以返回索引 1,其峰值元素为 2;
或者返回索引 5, 其峰值元素为 6。
作者:力扣 (LeetCode) 链接:leetcode-cn.com/leetbook/re…
来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
二、思路
循环取值
- 定义初始下标值index = 0, 长度len = nums.length
- 使用for循环,利用++i的特性 当前i值对比index值,如果当前i值大于index值的话
- 将i 赋值给index
- 其实相当于i与i-1对比,这时候得到的就是一个第一个向上的值,将数组循环到最后,那么index值就是最大的峰值
递归处理思路
- 峰值的特点
- 当前值大于左右两边的值
- 因此会有以下几种情况
- 第一种是满足情况的
- nums[index - 1] < nums[index] > nums[index + 1]
- 满足条件,可以返回值
- 第二种情况
- nums[index - 1] < nums[index] < nums[index + 1]
- 说明右边是最高值,那么就继续向右移动
- 第三种情况
- nums[index - 1] > nums[index] > nums[index + 1]
- 说明左边是最高值,那么就向左边移动
- 第四种情况
- nums[index - 1] > nums[index] < nums[index + 1]
- 这种情况,就更加兴趣爱好,随便选一边
- 以上四种情况,需要分步骤处理
- 定义随机的峰值,处理峰值的溢出问题,比如峰值不能为0和length
- 定义 get函数,传入数组和下标,判断下标不能为0和length
- 因此如果触发条件时,返回[0, 0], 否则返回[1,nums[index]]
- 之后判断峰值左右两边的条件
- [index - 1, index]和[index, index + 1]
- 判断条件
-
如果nums[index - 1] > nums[index] 为 true 否则为false -
如果nums[index] > nums[index + 1] 为 true 否则为false - 判断下边界值,如果出现边界值则返回[0, 0]
- 如果是边界值0,则返回false
- 如果边界值是len,则返回ture
- 递归处理
- 根据上述条件,处理函数
- 当函数不满足于nums[index] > nums[index + 1] 为true且nums[index - 1] < nums[index]为false时进入循环
- 下标值得左右移动规律
- nums[index] > nums[index + 1] 为true时 index -= 1
- 否则index += 1
- */
二分查找
- 二分查找
- 基于递归处理
- 还是选中间值,但是这个中间值是由mid = Math.floor((right - left)/2) 产生
- 其他的和思路二基本一致
三、代码实现
循环取值代码实现
let findPeakElement = function(nums) {
/**
* 定义初始下标值index = 0, 长度len = nums.length
* 使用for循环,利用++i的特性 当前i值对比index值,如果当前i值大于index值的话
* 将i 赋值给index
* 其实相当于i与i-1对比,这时候得到的就是一个第一个向上的值,将数组循环到最后,那么index值就是最大的峰值
* */
let len = nums.length, index = 0
for(let i = 0; i < len; ++i) {
if(nums[i] > nums[index]) {
index = i
}
}
return index
}
递归处理
let findPeakElement = function(nums) {
/**
* 峰值的特点
* 当前值大于左右两边的值
*
* 因此会有以下几种情况
* 第一种是满足情况的
* nums[index - 1] < nums[index] > nums[index + 1]
* 满足条件,可以返回值
*
* 第二种情况
* nums[index - 1] < nums[index] < nums[index + 1]
* 说明右边是最高值,那么就继续向右移动
*
* 第三种情况
* nums[index - 1] > nums[index] > nums[index + 1]
* 说明左边是最高值,那么就向左边移动
*
* 第四种情况
* nums[index - 1] > nums[index] < nums[index + 1]
* 这种情况,就更加兴趣爱好,随便选一边
*
*
* 以上四种情况,需要分步骤处理
* 定义随机的峰值,处理峰值的溢出问题,比如峰值不能为0和length
* 定义 get函数,传入数组和下标,判断下标不能为0和length
* 因此如果触发条件时,返回[0, 0], 否则返回[1,nums[index]]
*
* 之后判断峰值左右两边的条件
* [index - 1, index]和[index, index + 1]
* 判断条件 如果nums[index - 1] > nums[index] 为 true 否则为false
* 如果nums[index] > nums[index + 1] 为 true 否则为false
* 判断下边界值,如果出现边界值则返回[0, 0]
* 如果是边界值0,则返回false
* 如果边界值是len,则返回ture
*
* 判断峰值相同得结果,比如[1,2,2,1]
* 当存在这种清理,同样返回false
*
* 递归处理
* 根据上述条件,处理函数
* 当函数不满足于nums[index] > nums[index + 1] 为true且nums[index - 1] < nums[index]为false时进入循环
* 下标值得左右移动规律
* nums[index] > nums[index + 1] 为true时 index -= 1
* 否则index += 1
* */
const len = nums.length
let ind = parseInt(Math.random() * len)
while (!(compare(nums, ind - 1, ind) == false && compare(nums, ind, ind + 1) == true)) {
if (compare(nums, ind, ind + 1) == false) {
ind += 1
} else {
ind -= 1
}
}
return ind
}
// 处理边界值
const get = (nums, ind) => {
return (ind === -1 || ind === nums.length) == true ? [0, 0]: [1, nums[ind]]
}
// 比较的方法
const compare = (nums, ind1, ind2) => {
const num1 = get(nums, ind1)
const num2 = get(nums, ind2)
// 左右边界值
if (num1[0] !== num2[0]) return num2[0] > num1[0] ? false : true
// 峰值相同得情况
if (num1[1] === num2[1]) return false
// 正常情况
return num2[1] > num1[1] ? false : true
}
findPeakElement(nums)
二分查找
let findPeakElement = function(nums) {
/**
* 二分查找
* 基于递归处理
* 还是选中间值,但是这个中间值是由mid = Math.floor((right - left)/2) 产生
* 其他的和思路二基本一致
* */
let len = nums.length, left = 0, right = len - 1, ind = -1
while(left <= right) {
let mid = Math.floor((right - left)/2)
if(compare(nums, mid - 1, mid) == false && compare(nums, mid, mid + 1) == true) {
ind = mid
break
}
if (compare(nums, ind, ind + 1) == true) {
right = mid - 1
} else {
left = mid + 1
}
}
return ind
}
// 处理边界值
const get = (nums, ind) => {
return (ind === -1 || ind === nums.length) == true ? [0, 0]: [1, nums[ind]]
}
// 比较的方法
const compare = (nums, ind1, ind2) => {
const num1 = get(nums, ind1)
const num2 = get(nums, ind2)
// 左右边界值
if (num1[0] !== num2[0]) return num2[0] > num1[0] ? false : true
// 峰值相同得情况
if (num1[1] === num2[1]) return false
// 正常情况
return num2[1] > num1[1] ? false : true
}
findPeakElement(nums)