题解 | #合并表记录#

合并表记录

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

import java.util.*; public class Main { public static void main(String [] args){ Scanner sc = new Scanner(System.in); Map<Integer, Integer> map = new HashMap<>(); int num = sc.nextInt(); Set set = new TreeSet<>(); sc.nextLine(); for (int i = num; i > 0; i--) { String str= sc.nextLine(); String[] s= str.split(" "); int key=Integer.valueOf(s[0]); int value=Integer.valueOf(s[1]); set.add(key); if (map.get(key) != null) { map.put(key, map.get(key) +value); } else { map.put(key,value); } } for(int i :set){ System.out.print(i+" "); System.out.println(map.get(i)); }

  }

}

全部评论

相关推荐

昨天 13:52
门头沟学院 后端
给🐭🐭个面试机会吧:嘿,mvbatis
点赞 评论 收藏
分享
头发暂时没有的KFC总裁:找廉价劳动力罢了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务