题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.LinkedList; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); in.useDelimiter("\n"); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int count = in.nextInt(); List<String> list = new LinkedList<>(); outer: for(int i = 1; i <= count; i ++) { String key_value = in.next(); String[] split = key_value.split(" "); int key = Integer.valueOf(split[0]); int value = Integer.valueOf(split[1]); for(int j = 0; j < list.size(); j ++) { String s = list.get(j); String[] split1 = s.split(" "); int currKey = Integer.valueOf(split1[0]); int currVal = Integer.valueOf(split1[1]); if(currKey == key) { list.set(j, key + " " + (currVal + value) ); continue outer; } } list.add(key + " " + value); } list.sort(((o1, o2) -> Integer.valueOf(o1.split(" ")[0]) - Integer.parseInt(o2.split(" ")[0]))); for(String s : list) { System.out.println(s); } } } }