1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
Map<Integer, int[]> playerHistory = new HashMap<>();
for (int[] match : matches) {
playerHistory.putIfAbsent(match[0], new int[2]);
playerHistory.putIfAbsent(match[1], new int[2]);
playerHistory.get(match[0])[0] += 1;
playerHistory.get(match[1])[1] += 1;
}
List<Integer> noLosePlayers = new ArrayList<>();
List<Integer> oneLosePlayers = new ArrayList<>();
for (Map.Entry<Integer, int[]> entry : playerHistory.entrySet()) {
if (entry.getValue()[1] == 0) {
noLosePlayers.add(entry.getKey());
} else if (entry.getValue()[1] == 1) {
oneLosePlayers.add(entry.getKey());
}
}
Collections.sort(noLosePlayers);
Collections.sort(oneLosePlayers);
List<List<Integer>> ans = new ArrayList<>();
ans.add(noLosePlayers);
ans.add(oneLosePlayers);
return ans;
}
}

思路很直接. 统计每个数字的输的场数即可.

时间复杂度: O(nlogn)
空间复杂度: O(n)