题解 | #连续的牛群标签序列#

连续的牛群标签序列

https://www.nowcoder.com/practice/5db36ae74c274176a0cf9274e9f9ed3e

import java.util.*;


import java.util.*;
 
 
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param tag int整型一维数组
     * @return int整型
     */
    public int longestConsecutive (int[] tag) {
        // write code here
        int max = 0;
        for (int i = 0; i < tag.length; i++) {
            if(tag[i] > max)
                max = tag[i];
        }
 
        int[] hash = new int[max + 1];
        for (int i = 0; i < tag.length; i++) {
            hash[tag[i]]++;
        }
        max = 0;
        int temp = 0;
        for (int i = 0; i < hash.length; i++) {
            temp = hash[i] == 1 ? temp + 1 : 0;
            max = Math.max(max , temp);
        }
        return max;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务