You are given an integer array matches where matches[i] = [winneri, loseri] indicates that the player winneri defeated player loseri in a match.
Return a list answer of size 2 where:
answer[0] is a list of all players that have not lost any matches.
answer[1] is a list of all players that have lost exactly one match.
The values in the two lists should be returned in increasing order.
Note:
You should only consider the players that have played at least one match.
The testcases will be generated such that no two matches will have the same outcome.
class Solution {
public List<List<Integer>> findWinners(int[][] matches) {
/**
用一个数组记录每个人输赢的场次,初始为0
赢了且原始值>=0,赋新值为1
输了且原始值>=0,赋新值为-1
输了且原始值<0,原始值减一
*/
int n = matches.length;
int[] tmp = new int[200000];
for( int i = 0; i<n; i++ ) {
if(tmp[matches[i][0]] >= 0) tmp[matches[i][0]] = 1;
if(tmp[matches[i][1]] >= 0) {
tmp[matches[i][1]] = -1;
} else {
tmp[matches[i][1]] --;
}
}
List<List<Integer>> ans = new ArrayList();
List<Integer> p1 = new ArrayList();
List<Integer> p2 = new ArrayList();
for(int i=1; i < 110000; i++) {
if(tmp[i] == 1) {
p1.add(i);
}
if(tmp[i] == -1) {
p2.add(i);
}
}
ans.add(p1);
ans.add(p2);
return ans;
}
}