|
65 | 65 |
|
66 | 66 | <!-- 这里可写通用的实现逻辑 -->
|
67 | 67 |
|
| 68 | +**方法一:哈希表** |
| 69 | + |
68 | 70 | <!-- tabs:start -->
|
69 | 71 |
|
70 | 72 | ### **Python3**
|
71 | 73 |
|
72 | 74 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
73 | 75 |
|
74 | 76 | ```python
|
75 |
| - |
| 77 | +class Solution: |
| 78 | + def findWinners(self, matches: List[List[int]]) -> List[List[int]]: |
| 79 | + cnt = Counter() |
| 80 | + for a, b in matches: |
| 81 | + if a not in cnt: |
| 82 | + cnt[a] = 0 |
| 83 | + cnt[b] += 1 |
| 84 | + ans = [[], []] |
| 85 | + for u, v in cnt.items(): |
| 86 | + if v < 2: |
| 87 | + ans[v].append(u) |
| 88 | + ans[0].sort() |
| 89 | + ans[1].sort() |
| 90 | + return ans |
76 | 91 | ```
|
77 | 92 |
|
78 | 93 | ### **Java**
|
79 | 94 |
|
80 | 95 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
81 | 96 |
|
82 | 97 | ```java
|
| 98 | +class Solution { |
| 99 | + public List<List<Integer>> findWinners(int[][] matches) { |
| 100 | + Map<Integer, Integer> cnt = new HashMap<>(); |
| 101 | + for (int[] m : matches) { |
| 102 | + int a = m[0], b = m[1]; |
| 103 | + cnt.putIfAbsent(a, 0); |
| 104 | + cnt.put(b, cnt.getOrDefault(b, 0) + 1); |
| 105 | + } |
| 106 | + List<List<Integer>> ans = new ArrayList<>(); |
| 107 | + ans.add(new ArrayList<>()); |
| 108 | + ans.add(new ArrayList<>()); |
| 109 | + for (Map.Entry<Integer, Integer> entry : cnt.entrySet()) { |
| 110 | + int u = entry.getKey(); |
| 111 | + int v = entry.getValue(); |
| 112 | + if (v < 2) { |
| 113 | + ans.get(v).add(u); |
| 114 | + } |
| 115 | + } |
| 116 | + Collections.sort(ans.get(0)); |
| 117 | + Collections.sort(ans.get(1)); |
| 118 | + return ans; |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### **C++** |
| 124 | + |
| 125 | +```cpp |
| 126 | +class Solution { |
| 127 | +public: |
| 128 | + vector<vector<int>> findWinners(vector<vector<int>>& matches) { |
| 129 | + unordered_map<int, int> cnt; |
| 130 | + for (auto& m : matches) |
| 131 | + { |
| 132 | + int a = m[0], b = m[1]; |
| 133 | + if (!cnt.count(a)) cnt[a] = 0; |
| 134 | + ++cnt[b]; |
| 135 | + } |
| 136 | + vector<vector<int>> ans(2); |
| 137 | + for (auto& [u, v] : cnt) |
| 138 | + { |
| 139 | + if (v < 2) ans[v].push_back(u); |
| 140 | + } |
| 141 | + sort(ans[0].begin(), ans[0].end()); |
| 142 | + sort(ans[1].begin(), ans[1].end()); |
| 143 | + return ans; |
| 144 | + } |
| 145 | +}; |
| 146 | +``` |
83 | 147 |
|
| 148 | +### **Go** |
| 149 | +
|
| 150 | +```go |
| 151 | +func findWinners(matches [][]int) [][]int { |
| 152 | + cnt := map[int]int{} |
| 153 | + for _, m := range matches { |
| 154 | + a, b := m[0], m[1] |
| 155 | + if _, ok := cnt[a]; !ok { |
| 156 | + cnt[a] = 0 |
| 157 | + } |
| 158 | + cnt[b]++ |
| 159 | + } |
| 160 | + ans := make([][]int, 2) |
| 161 | + for u, v := range cnt { |
| 162 | + if v < 2 { |
| 163 | + ans[v] = append(ans[v], u) |
| 164 | + } |
| 165 | + } |
| 166 | + sort.Ints(ans[0]) |
| 167 | + sort.Ints(ans[1]) |
| 168 | + return ans |
| 169 | +} |
84 | 170 | ```
|
85 | 171 |
|
86 | 172 | ### **TypeScript**
|
|
0 commit comments