双Map分别记录上一层key-value和本层key-value

特征提取

http://www.nowcoder.com/questionTerminal/5afcf93c419a4aa793e9b325d01957e2

import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        //num个例子
        for (int k = 0 ; k < num ; k++) {
            //判断几行案例
            int n = sc.nextInt();
            //记录上一行键值对的 map
            HashMap<String , Integer> map = new HashMap<>();
            //记录本行键值对的 map
            HashMap<String , Integer> temp = new HashMap<>();

            int max = 1;
            //每行案例
            for (int i = 0 ; i < n ; i++) {
                //每一行都几个键值对
                int len = sc.nextInt();
                temp.clear();
                for (int j = 0 ; j < len ; j++) {
                     String s = sc.nextInt() + " " + sc.nextInt();
                     temp.put(s , temp.getOrDefault(s , 0) + 1);
                }

                Set<String> set = map.keySet();
                for (String key : set) {
                    if (temp.containsKey(key)) {
                        //把上一层的有效的轨迹加到这一层
                        temp.put(key , temp.get(key) + map.get(key));
                        max = Math.max(max , temp.get(key));
                    }    
                }
                map.clear();
                map.putAll(temp);
            }
            System.out.print(max);

         } 
    }
}
全部评论
1 8 2 1 1 1 1 2 1 1 1 4 2 1 1 2 2 2 2 2 1 4 0 0 1 1 1 1 1 1 这个测试用例大佬写的通过不了哎,原因在于使用HashMap对相同的key对应的value进行了多次统计,但正好测试用例没有这种情况
点赞 回复 分享
发布于 2022-02-26 20:14

相关推荐

把实习生当正职使昨天第一天就加班,晚上连口饭都没吃上,以后日子咋过,我不想干了
码农索隆:实习不怕忙,就怕干的活重复且没难度,要干就干那种有深度有难度的任务,这样才能快速的提升
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

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