题解 | #连续的牛群标签序列#
连续的牛群标签序列
https://www.nowcoder.com/practice/5db36ae74c274176a0cf9274e9f9ed3e
import java.util.*;
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param tag int整型一维数组
     * @return int整型
     */
    public int longestConsecutive (int[] tag) {
        // write code here
        Set<Integer> set = new HashSet<>();
        for (int i = 0; i < tag.length; i++) {
            set.add(tag[i]);
        }
        int ans = 0;
        for (Integer value : set) {
            int count = 1;
            while (set.contains(value + 1)) {
                count++;
                value++;
            }
            ans = Math.max(ans, count);
        }
        return ans;
    }
}
本题考察的知识点是哈希表的应用,所用编程语言是java。
我采用的是双重循环,并不是0(n)时间复杂度
查看16道真题和解析



