题解 | #合并表记录#

合并表记录

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

import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        TreeMap<Long,Long> map = new TreeMap<Long,Long>();
        int n = in.nextInt();
        long x,y;
        while(n>0){
            x = in.nextLong();
            y = in.nextLong();
            if(map.containsKey(x)){
                map.put(x,map.get(x)+y);
            }else{
                map.put(x,y);
            }
            n--;
        }
        for(Long key : map.keySet()){
            System.out.println(key+" "+map.get(key));
        }
    }
}

全部评论
TreeMap换成HashMap,会不会好点
点赞 回复 分享
发布于 2023-05-28 20:21 山东
牛啊牛啊兄嘚🤨,这个我拿走收藏了
点赞 回复 分享
发布于 2023-05-28 21:09 甘肃

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务