2225. Find Players With Zero or One Losses
给你一个阵列matches,每个阵列里面有两个数字,第一个数字表示赢家编号,第二
个数字找出输家编号,回传一个List分别包含了没输过的玩家编号和只输过一场的玩
家编号,玩家编号需要升序排列。
Example:
Input: matches =
[[1,3],[2,3],[3,6],[5,6],[5,7],[4,5],[4,8],[4,9],[10,4],[10,9]]
Output: [[1,2,10],[4,5,7,8]]
Explanation:
Players 1, 2, and 10 have not lost any matches.
Players 4, 5, 7, and 8 each have lost one match.
Players 3, 6, and 9 each have lost two matches.
Thus, answer[0] = [1,2,10] and answer[1] = [4,5,7,8].
Constraints:
1 <= matches.length <= 10^5
matches[i].length == 2
1 <= winneri, loseri <= 10^5
winner[i] != loser[i]
All matches[i] are unique.
方法一 TreeMap
思路:
1.首先,因为题目要找只输过一场和没输过的玩家,所以我们要把所有(赢和输)
的数字都加入到一个集合里面,并去除输超过一场的玩家,赢的次数不用统计只
要统计输的即可。
2.再来,因为题目要求要排序所以我们统计每个玩家输的次数,插入一个有序Map之中
3.遍历Map从ID小的玩家编号开始插入answer,没输过为0的放第一个,只输一场为1的
放第二个,其他不满足条件的不插入。
JavaCode: