题解 | #牧场里的编号顺序#

牧场里的编号顺序

https://www.nowcoder.com/practice/6741b77f486a493da5258738323ddd3e

题目考察的知识点是:

本题主要考察知识点是哈希表。

题目解答方法的文字分析:

通过循环去获取最大连接数,然后用三元运算符获取结果

本题解析所用的编程语言:

java语言。

完整且正确的编程代码:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param ids int整型一维数组
     * @param n int整型
     * @return int整型
     */
    public int longestConsecutive (int[] ids, int n) {
        // write code here
        int maxLength = 0;
        int currentLength = 1;
        for (int i = 1; i < n; i++) {
            if (ids[i] > ids[i - 1]) {
                currentLength++;
            } else {
                maxLength = maxLength > currentLength ? maxLength : currentLength;
                currentLength = 1;
            }
        }
        return maxLength > currentLength ? maxLength : currentLength;
    }
}

#题解#
全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务