题解 | #合并表记录#

合并表记录

http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201

TreeMap 来处理合并和排序。

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int size = sc.nextInt();
        // merge value by key, tree map can handle sorting
        Map<Integer, Integer> numMap = new TreeMap<>();
        for (int i = 0; i < size; i++) {
            int key = sc.nextInt();
            int value = numMap.getOrDefault(key, 0);
            value += sc.nextInt();
            numMap.put(key, value);
        }

        for (Map.Entry<Integer, Integer> entry : numMap.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }
}
全部评论

相关推荐

故事和酒66:小米现在校招很多都是在高校搞小米训练营,然后直接挑人,大四就去实习,所以实际上校招总名额是变少了的。同学211本无经验经过两周培训直接签了
秋招,不懂就问
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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