Skip to content

Latest commit

 

History

History
35 lines (30 loc) · 847 Bytes

2022-11-28.md

File metadata and controls

35 lines (30 loc) · 847 Bytes

题目

  1. Find Players With Zero or One Losses

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.

思路

代码

var findWinners = function(matches) {
  const losers = {}
  for (const match of matches) {
    if (!(match[0] in losers)) {
      losers[match[0]] = 0
    }
    if (!(match[1] in losers)) {
      losers[match[1]] = 0
    }
    losers[match[1]]++
  }
  const answer = [[], []]
  for (const i in losers) {
    const count = losers[i]
    if (count == 0 || count == 1) {
      answer[count].push(i)
    }
  }
  return answer
};