@@ -47,30 +47,102 @@ So team 1 will be the champion.
47
47
48
48
## Solutions
49
49
50
+ ** Solution 1: Enumeration**
51
+
52
+ We can enumerate each team $i$. If team $i$ has won every match, then team $i$ is the champion, and we can directly return $i$.
53
+
54
+ The time complexity is $O(n^2),ドル where $n$ is the number of teams. The space complexity is $O(1)$.
55
+
50
56
<!-- tabs:start -->
51
57
52
58
### ** Python3**
53
59
54
60
``` python
55
-
61
+ class Solution :
62
+ def findChampion (self , grid : List[List[int ]]) -> int :
63
+ for i, row in enumerate (grid):
64
+ if all (x == 1 for j, x in enumerate (row) if i != j):
65
+ return i
56
66
```
57
67
58
68
### ** Java**
59
69
60
70
``` java
61
-
71
+ class Solution {
72
+ public int findChampion (int [][] grid ) {
73
+ int n = grid. length;
74
+ for (int i = 0 ;; ++ i) {
75
+ int cnt = 0 ;
76
+ for (int j = 0 ; j < n; ++ j) {
77
+ if (i != j && grid[i][j] == 1 ) {
78
+ ++ cnt;
79
+ }
80
+ }
81
+ if (cnt == n - 1 ) {
82
+ return i;
83
+ }
84
+ }
85
+ }
86
+ }
62
87
```
63
88
64
89
### ** C++**
65
90
66
91
``` cpp
67
-
92
+ class Solution {
93
+ public:
94
+ int findChampion(vector<vector<int >>& grid) {
95
+ int n = grid.size();
96
+ for (int i = 0;; ++i) {
97
+ int cnt = 0;
98
+ for (int j = 0; j < n; ++j) {
99
+ if (i != j && grid[ i] [ j ] == 1) {
100
+ ++cnt;
101
+ }
102
+ }
103
+ if (cnt == n - 1) {
104
+ return i;
105
+ }
106
+ }
107
+ }
108
+ };
68
109
```
69
110
70
111
### **Go**
71
112
72
113
```go
114
+ func findChampion(grid [][]int) int {
115
+ n := len(grid)
116
+ for i := 0; ; i++ {
117
+ cnt := 0
118
+ for j, x := range grid[i] {
119
+ if i != j && x == 1 {
120
+ cnt++
121
+ }
122
+ }
123
+ if cnt == n-1 {
124
+ return i
125
+ }
126
+ }
127
+ }
128
+ ```
73
129
130
+ ### ** TypeScript**
131
+
132
+ ``` ts
133
+ function findChampion(grid : number [][]): number {
134
+ for (let i = 0 , n = grid .length ; ; ++ i ) {
135
+ let cnt = 0 ;
136
+ for (let j = 0 ; j < n ; ++ j ) {
137
+ if (i !== j && grid [i ][j ] === 1 ) {
138
+ ++ cnt ;
139
+ }
140
+ }
141
+ if (cnt === n - 1 ) {
142
+ return i ;
143
+ }
144
+ }
145
+ }
74
146
```
75
147
76
148
### ** ...**
0 commit comments