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

寻找牛群中的最高牛

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

知识点

数组

解题思路

根据题目意思就是寻找最大值下标,定义一个max保存最大值,ans保存最大值下标。遍历数组维护这两个值。

Java题解

import java.util.*;


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

全部评论

相关推荐

xxxxOxo:该催就催,想要你的不会因为催就挂,催了就挂的是因为本来就要挂你
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务