双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

相关推荐

03-03 10:35
3d人士会梦见住进比弗利山庄吗:这四个项目属于是初学者的玩具了。不知道面试官咋问,而且双非本搞算法除了9,还是保守至少c9
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务