Find Players With Zero or One Loss
给一个数组,里面是比赛结果[winner, loser], 求两个数组, 第一个是没有输过人的比赛, 第二个是只输了一场的比赛. 要求这个人起码参赛了.
首先判断是否参赛, 用set, 然后记录比赛的loser情况, 即可.
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 |
class Solution { public List<List<Integer>> findWinners(int[][] matches) { int[] check = new int[200000]; Set<Integer> set = new HashSet<>(); for(int[] m : matches){ set.add(m[0]); set.add(m[1]); check[m[1]]++; } List<Integer> zero = new ArrayList<>(); List<Integer> one = new ArrayList<>(); List<List<Integer>> res = new ArrayList<>(); for(int i = 0; i < check.length; i++){ if(!set.contains(i)) continue; if(check[i] == 1) one.add(i); else if(check[i] == 0) zero.add(i); } res.add(zero); res.add(one); return res; } } |