9月3日百度笔试第二题,用数组hash,怎么就过不了,求指教

// 本题为考试单行多行输入输出规范示例,无需提交,不计分。
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        in.nextLine();
        while (in.hasNextInt()) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
            int n = in.nextInt();
            int m = in.nextInt();
            int s = m;
            in.nextLine();
            int[] hash = new int[n+1];
            while (m>0){
                int tempCount = in.nextInt();
                in.nextLine();
                while (tempCount>0){
                    int start = in.nextInt();
                    int end = in.nextInt();
                    in.nextLine();
                    for (int i=start; i<=end; i++){
                        hash[i]++;
                    }
                    tempCount--;
                }
                m--;
            }
            int resultCount = 0;
            ArrayList<Integer> list = new ArrayList<>();
            for(int i=0;i<hash.length;i++){
                if(hash[i]==s){
                    resultCount++;
                    list.add(i);
                }
            }
            System.out.println(resultCount);
            for(int i=0;i<list.size()-1;i++){
                System.out.print(list.get(i)+" ");
            }
            System.out.println(list.get(list.size()-1));
        }
    }
}
难道是没用long吗,不应该啊,题目中给范围了啊,都是在int范围内的啊

#笔试题目##百度#
全部评论
我这个提示数组越界了
点赞 回复 分享
发布于 2020-09-03 21:22
着急代码改的有些简陋
点赞 回复 分享
发布于 2020-09-03 21:24
难道是超时了吗
点赞 回复 分享
发布于 2020-09-03 21:25

相关推荐

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