题解 | #寻找牛群中的最高牛#

寻找牛群中的最高牛

https://www.nowcoder.com/practice/df826dd3f9304c61bb6e13fcf16652c3

考察知识点:数组

题目分析:

不会O(log(n))求多峰的最高峰,所以在这里就遍历一遍数组找最大值了。希望有大佬能解决这个问题捏

所用编程语言:C++

class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param heights int整型vector
     * @return int整型
     */
    int findPeakElement(vector<int>& heights) {
        // write code here
        int res = -1;
        int index = -1;
        int size = heights.size();
        for (int i = 0; i < size; i++) {
            if (heights[i] > res) {
                res = heights[i];
                index = i;
            }
        }
        return index;
    }
};

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务