题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); TreeMap<Integer, Integer> treeMap = new TreeMap<>(); int count = scanner.nextInt(); for (int i = 0; i < count; i++) { int index = scanner.nextInt(); int value = scanner.nextInt(); if (treeMap.containsKey(index)) { value = treeMap.remove(index) + value; treeMap.put(index, value); } else { treeMap.put(index, value); } } Set<Integer> integers = treeMap.keySet(); // TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>() { // @Override // public int compare(Integer o1, Integer o2) { // return o1.compareTo(o2); // } // }); for (Integer integer : integers) { System.out.println(integer + " " + treeMap.get(integer)); } } }