Skip to content

Commit 269636d

Browse files
committed
feat: add solutions to lc problem: No.0544
No.0544.Output Contest Matches
1 parent 8aa44a4 commit 269636d

File tree

6 files changed

+174
-2
lines changed

6 files changed

+174
-2
lines changed

solution/0500-0599/0544.Output Contest Matches/README.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,86 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
**方法一:模拟**
61+
62+
假设 `team[i]` 为当前轮次中第 i 强的队伍。
63+
64+
每一轮,将第 i 支队伍变成 `"(" + team[i] + "," + team[n-1-i] + ")"`,并且每一轮淘汰一半的队伍。
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**
6369

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

6672
```python
67-
73+
class Solution:
74+
def findContestMatch(self, n: int) -> str:
75+
team = [str(i + 1) for i in range(n)]
76+
while n > 1:
77+
for i in range(n >> 1):
78+
team[i] = f'({team[i]},{team[n - 1 - i]})'
79+
n >>= 1
80+
return team[0]
6881
```
6982

7083
### **Java**
7184

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

7487
```java
88+
class Solution {
89+
public String findContestMatch(int n) {
90+
String[] team = new String[n];
91+
for (int i = 0; i < n; ++i) {
92+
team[i] = "" + (i + 1);
93+
}
94+
for (; n > 1; n /= 2) {
95+
for (int i = 0; i < n / 2; ++i) {
96+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
97+
}
98+
}
99+
return team[0];
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
string findContestMatch(int n) {
110+
vector<string> team(n);
111+
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
112+
for (; n > 1; n >>= 1)
113+
{
114+
for (int i = 0; i < n >> 1; ++i)
115+
{
116+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
117+
}
118+
}
119+
return team[0];
120+
}
121+
};
122+
```
75123
124+
### **Go**
125+
126+
```go
127+
func findContestMatch(n int) string {
128+
team := make([]string, n)
129+
for i := range team {
130+
team[i] = strconv.Itoa(i + 1)
131+
}
132+
for n > 1 {
133+
for i := 0; i < n>>1; i++ {
134+
team[i] = "(" + team[i] + "," + team[n-1-i] + ")"
135+
}
136+
n >>= 1
137+
}
138+
return team[0]
139+
}
76140
```
77141

78142
### **...**

solution/0500-0599/0544.Output Contest Matches/README_EN.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,71 @@ Since the third round will generate the final winner, you need to output the ans
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def findContestMatch(self, n: int) -> str:
56+
team = [str(i + 1) for i in range(n)]
57+
while n > 1:
58+
for i in range(n >> 1):
59+
team[i] = f'({team[i]},{team[n - 1 - i]})'
60+
n >>= 1
61+
return team[0]
5562
```
5663

5764
### **Java**
5865

5966
```java
67+
class Solution {
68+
public String findContestMatch(int n) {
69+
String[] team = new String[n];
70+
for (int i = 0; i < n; ++i) {
71+
team[i] = "" + (i + 1);
72+
}
73+
for (; n > 1; n /= 2) {
74+
for (int i = 0; i < n / 2; ++i) {
75+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
76+
}
77+
}
78+
return team[0];
79+
}
80+
}
81+
```
82+
83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
string findContestMatch(int n) {
89+
vector<string> team(n);
90+
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
91+
for (; n > 1; n >>= 1)
92+
{
93+
for (int i = 0; i < n >> 1; ++i)
94+
{
95+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
96+
}
97+
}
98+
return team[0];
99+
}
100+
};
101+
```
60102
103+
### **Go**
104+
105+
```go
106+
func findContestMatch(n int) string {
107+
team := make([]string, n)
108+
for i := range team {
109+
team[i] = strconv.Itoa(i + 1)
110+
}
111+
for n > 1 {
112+
for i := 0; i < n>>1; i++ {
113+
team[i] = "(" + team[i] + "," + team[n-1-i] + ")"
114+
}
115+
n >>= 1
116+
}
117+
return team[0]
118+
}
61119
```
62120

63121
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string findContestMatch(int n) {
4+
vector<string> team(n);
5+
for (int i = 0; i < n; ++i) team[i] = to_string(i + 1);
6+
for (; n > 1; n >>= 1)
7+
{
8+
for (int i = 0; i < n >> 1; ++i)
9+
{
10+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
11+
}
12+
}
13+
return team[0];
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
func findContestMatch(n int) string {
2+
team := make([]string, n)
3+
for i := range team {
4+
team[i] = strconv.Itoa(i + 1)
5+
}
6+
for n > 1 {
7+
for i := 0; i < n>>1; i++ {
8+
team[i] = "(" + team[i] + "," + team[n-1-i] + ")"
9+
}
10+
n >>= 1
11+
}
12+
return team[0]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public String findContestMatch(int n) {
3+
String[] team = new String[n];
4+
for (int i = 0; i < n; ++i) {
5+
team[i] = "" + (i + 1);
6+
}
7+
for (; n > 1; n /= 2) {
8+
for (int i = 0; i < n / 2; ++i) {
9+
team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")";
10+
}
11+
}
12+
return team[0];
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def findContestMatch(self, n: int) -> str:
3+
team = [str(i + 1) for i in range(n)]
4+
while n > 1:
5+
for i in range(n >> 1):
6+
team[i] = f'({team[i]},{team[n - 1 - i]})'
7+
n >>= 1
8+
return team[0]

0 commit comments

Comments
 (0)