Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4bde36c

Browse files
feat: add solutions to lc problems: No.2923,2924 (#1933)
* No.2923.Find Champion I * No.2924.Find Champion II
1 parent 1f0097d commit 4bde36c

File tree

14 files changed

+448
-12
lines changed

14 files changed

+448
-12
lines changed

‎solution/2900-2999/2923.Find Champion I/README.md‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,106 @@ grid[1][2] == 1 表示 1 队比 2 队强。
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:枚举**
57+
58+
我们可以枚举每一支队伍 $i,ドル如果 $i$ 队的每一场比赛都赢了,那么 $i$ 队就是冠军,直接返回 $i$ 即可。
59+
60+
时间复杂度 $O(n^2),ドル其中 $n$ 是队伍数量。空间复杂度 $O(1)$。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
def findChampion(self, grid: List[List[int]]) -> int:
71+
for i, row in enumerate(grid):
72+
if all(x == 1 for j, x in enumerate(row) if i != j):
73+
return i
6474
```
6575

6676
### **Java**
6777

6878
<!-- 这里可写当前语言的特殊实现逻辑 -->
6979

7080
```java
71-
81+
class Solution {
82+
public int findChampion(int[][] grid) {
83+
int n = grid.length;
84+
for (int i = 0;; ++i) {
85+
int cnt = 0;
86+
for (int j = 0; j < n; ++j) {
87+
if (i != j && grid[i][j] == 1) {
88+
++cnt;
89+
}
90+
}
91+
if (cnt == n - 1) {
92+
return i;
93+
}
94+
}
95+
}
96+
}
7297
```
7398

7499
### **C++**
75100

76101
```cpp
77-
102+
class Solution {
103+
public:
104+
int findChampion(vector<vector<int>>& grid) {
105+
int n = grid.size();
106+
for (int i = 0;; ++i) {
107+
int cnt = 0;
108+
for (int j = 0; j < n; ++j) {
109+
if (i != j && grid[i][j] == 1) {
110+
++cnt;
111+
}
112+
}
113+
if (cnt == n - 1) {
114+
return i;
115+
}
116+
}
117+
}
118+
};
78119
```
79120
80121
### **Go**
81122
82123
```go
124+
func findChampion(grid [][]int) int {
125+
n := len(grid)
126+
for i := 0; ; i++ {
127+
cnt := 0
128+
for j, x := range grid[i] {
129+
if i != j && x == 1 {
130+
cnt++
131+
}
132+
}
133+
if cnt == n-1 {
134+
return i
135+
}
136+
}
137+
}
138+
```
83139

140+
### **TypeScript**
141+
142+
```ts
143+
function findChampion(grid: number[][]): number {
144+
for (let i = 0, n = grid.length; ; ++i) {
145+
let cnt = 0;
146+
for (let j = 0; j < n; ++j) {
147+
if (i !== j && grid[i][j] === 1) {
148+
++cnt;
149+
}
150+
}
151+
if (cnt === n - 1) {
152+
return i;
153+
}
154+
}
155+
}
84156
```
85157

86158
### **...**

‎solution/2900-2999/2923.Find Champion I/README_EN.md‎

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,102 @@ So team 1 will be the champion.
4747

4848
## Solutions
4949

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+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
```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
5666
```
5767

5868
### **Java**
5969

6070
```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+
}
6287
```
6388

6489
### **C++**
6590

6691
```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+
};
68109
```
69110
70111
### **Go**
71112
72113
```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+
```
73129

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+
}
74146
```
75147

76148
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int findChampion(vector<vector<int>>& grid) {
4+
int n = grid.size();
5+
for (int i = 0;; ++i) {
6+
int cnt = 0;
7+
for (int j = 0; j < n; ++j) {
8+
if (i != j && grid[i][j] == 1) {
9+
++cnt;
10+
}
11+
}
12+
if (cnt == n - 1) {
13+
return i;
14+
}
15+
}
16+
}
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func findChampion(grid [][]int) int {
2+
n := len(grid)
3+
for i := 0; ; i++ {
4+
cnt := 0
5+
for j, x := range grid[i] {
6+
if i != j && x == 1 {
7+
cnt++
8+
}
9+
}
10+
if cnt == n-1 {
11+
return i
12+
}
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public int findChampion(int[][] grid) {
3+
int n = grid.length;
4+
for (int i = 0;; ++i) {
5+
int cnt = 0;
6+
for (int j = 0; j < n; ++j) {
7+
if (i != j && grid[i][j] == 1) {
8+
++cnt;
9+
}
10+
}
11+
if (cnt == n - 1) {
12+
return i;
13+
}
14+
}
15+
}
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution:
2+
def findChampion(self, grid: List[List[int]]) -> int:
3+
for i, row in enumerate(grid):
4+
if all(x == 1 for j, x in enumerate(row) if i != j):
5+
return i
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function findChampion(grid: number[][]): number {
2+
for (let i = 0, n = grid.length; ; ++i) {
3+
let cnt = 0;
4+
for (let j = 0; j < n; ++j) {
5+
if (i !== j && grid[i][j] === 1) {
6+
++cnt;
7+
}
8+
}
9+
if (cnt === n - 1) {
10+
return i;
11+
}
12+
}
13+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /