题解 | #合并表记录#

合并表记录

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

//放入map, 对map的key进行判断,key有重复的就重新put进map更新对应的value的值、
//重点是数据结构的选择,
//这里没用hashmap, 而是用的TreeMap ,因为hashmap是无序的,TreeMap是有序的,
//当key的数量非常多的时候,TreeMap可以保证有序。
import java.util.*;
public class Main{
    public static void  main(String[] args){
        Scanner sc = new Scanner(System.in);
        TreeMap<Integer,Integer> outMap = new TreeMap<Integer,Integer>();
        //对输入数据用空格切割
        while(sc.hasNext()){
           String inStr = sc.nextLine();
           String[] arr  = inStr.split(" ");
            if(arr.length>1){
               int key = Integer.parseInt(arr[0]);
               int value = Integer.parseInt(arr[1]);
                if(!outMap.containsKey(key)){
                    outMap.put(key,value);
                }else{
                    outMap.put(key,outMap.get(key) + value);
                }
            }
        }
               //遍历map输出
//                for(String key :outMap.keySet()){
//                    System.out.println(key +" " +outMap.get(key));
//                }
            for (Map.Entry<Integer, Integer> entry : outMap.entrySet()) {
                    System.out.println(entry.getKey()+" "+entry.getValue());
            }
    }
}
全部评论

相关推荐

程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
06-12 17:46
门头沟学院 Java
运营你豪哥:来说重点: ​1.项目前置,时间倒序。​​ 2.​项目描述强化结果与量化效果(STAR原则里的R)。​​ ​3.个人技能精炼,明确掌握程度,突出核心。​​ ​4.增加强有力开头的个人总结部分。​​ 5.​优化教育背景(成绩排名)、合并奖项与活动。​​
听劝,我这个简历该怎么改...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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