162 Find Peak Element 寻找峰值

峰值元素是指其值大于左右相邻值的元素。
给定一个输入数组,其中 num[i] ≠ num[i+1],找到峰值元素并返回其索引。
数组可能包含多个峰值,在这种情况下,返回到任何一个峰值所在位置都可以。
你可以想象得到  num[-1] = num[n] = -∞。
例如,在数组 [1, 2, 3, 1]中 3 是峰值元素您的函数应该返回索引号2。
注意:
你的解决方案应该是对数复杂度的。

详见:https://leetcode.com/problems/find-peak-element/description/

class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int size=nums.size();
        if(size==0||nums.empty())
        {
            return -1;
        }
        int left=0;
        int right=size-1;
        int mid=0;
        while(left<right)
        {
            mid=(left+right)/2;
            if(nums[mid]<nums[mid+1])
            {
                left=mid+1;
            }
            else
            {
                right=mid;
            }
        }
        return right;
    }
};

原文地址:https://www.cnblogs.com/xidian2014/p/8728346.html

时间: 2024-10-13 06:43:38

162 Find Peak Element 寻找峰值的相关文章

LeetCode:Find Peak Element - 寻找一个数组内的顶点

1.题目名称 Find Peak Element(寻找一个数组内的顶点) 2.题目地址 https://leetcode.com/problems/find-peak-element/ 3.题目内容 英文: 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

LeetCode 162. Find Peak Element (找到峰值)

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 fi

leetcode 162. Find Peak Element --------- java

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 fi

LeetCode 162. Find Peak Element 20170706

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 fi

LeetCode OJ 162. Find Peak Element

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 fi

162. Find Peak Element (Array; Divide-and-Conquer)

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 fi

Leetcode 162 Find Peak Element (二分查找思想)

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 fi

162.Find Peak Element(二分查找)

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 anyone of the peaks is fin

leetcode[162]Find Peak Element

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 fi