Skip to content

Commit 6757a1f

Browse files
committed
feat: add solutions to leetcode problem: No.1275. Find Winner on a Tic Tac Toe Game
1 parent 1b63efc commit 6757a1f

File tree

5 files changed

+158
-57
lines changed

5 files changed

+158
-57
lines changed

solution/1200-1299/1275.Find Winner on a Tic Tac Toe Game/README.md

+60-2
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,85 @@
7979
<li><code>moves</code> 遵循井字棋的规则。</li>
8080
</ul>
8181

82-
8382
## 解法
8483

8584
<!-- 这里可写通用的实现逻辑 -->
8685

86+
判断 A、B 谁能获胜,只需判断最后一个落棋的人能否获胜即可。我们用数组 `counter` 记录 `0~2` 行、`0~2` 列、`正对角线``副对角线`是否已满 3 个棋子。如果等于 3,此人获胜,游戏结束。
87+
88+
若最后落棋者为未能获胜,棋盘被下满返回 `Draw`,未下满则返回 `Pending`
89+
90+
> 数组 `counter` 长度为 8,其中,`counter[0..2]` 对应 `0~2` 行,`counter[3..5]` 对应 `0~2` 列,`counter[6]` 对应正对角线,`counter[7]` 对应副对角线的落棋次数。
91+
8792
<!-- tabs:start -->
8893

8994
### **Python3**
9095

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

9398
```python
94-
99+
class Solution:
100+
def tictactoe(self, moves: List[List[int]]) -> str:
101+
n = len(moves)
102+
counter = [0] * 8
103+
for i in range(n - 1, -1, -2):
104+
row, col = moves[i][0], moves[i][1]
105+
counter[row] += 1
106+
counter[col + 3] += 1
107+
if row == col:
108+
counter[6] += 1
109+
if row + col == 2:
110+
counter[7] += 1
111+
if counter[row] == 3 or counter[col + 3] == 3 or counter[6] == 3 or counter[7] == 3:
112+
return "A" if (i % 2) == 0 else "B"
113+
return "Draw" if n == 9 else "Pending"
95114
```
96115

97116
### **Java**
98117

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

101120
```java
121+
class Solution {
122+
public String tictactoe(int[][] moves) {
123+
int n = moves.length;
124+
int[] counter = new int[8];
125+
for (int i = n - 1; i >= 0; i -= 2) {
126+
int row = moves[i][0], col = moves[i][1];
127+
++counter[row];
128+
++counter[col + 3];
129+
if (row == col) ++counter[6];
130+
if (row + col == 2) ++counter[7];
131+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
132+
return (i % 2) == 0 ? "A" : "B";
133+
}
134+
}
135+
return n == 9 ? "Draw" : "Pending";
136+
}
137+
}
138+
```
102139

140+
### **C++**
141+
142+
```cpp
143+
class Solution {
144+
public:
145+
string tictactoe(vector<vector<int>>& moves) {
146+
int n = moves.size();
147+
vector<int> counter(8, 0);
148+
for (int i = n - 1; i >= 0; i -= 2) {
149+
int row = moves[i][0], col = moves[i][1];
150+
++counter[row];
151+
++counter[col + 3];
152+
if (row == col) ++counter[6];
153+
if (row + col == 2) ++counter[7];
154+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
155+
return (i % 2 == 0) ? "A" : "B";
156+
}
157+
}
158+
return n == 9 ? "Draw" : "Pending";
159+
}
160+
};
103161
```
104162
105163
### **...**

solution/1200-1299/1275.Find Winner on a Tic Tac Toe Game/README_EN.md

+54-31
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
<p>Tic-tac-toe is played&nbsp;by&nbsp;two players <em>A</em> and <em>B</em> on a&nbsp;<i>3</i>&nbsp;x&nbsp;<i>3</i>&nbsp;grid.</p>
88

9-
10-
119
<p>Here are the rules of Tic-Tac-Toe:</p>
1210

13-
14-
1511
<ul>
1612
<li>Players take turns placing characters into empty squares (&quot; &quot;).</li>
1713
<li>The first player <em>A</em> always places &quot;X&quot; characters, while the second player <em>B</em>&nbsp;always places &quot;O&quot; characters.</li>
@@ -21,26 +17,16 @@
2117
<li>No more moves can be played if the game is over.</li>
2218
</ul>
2319

24-
25-
2620
<p>Given an array <code>moves</code> where each element&nbsp;is another array of size 2 corresponding to the row and column of the grid where they mark their respective character in the order in which <em>A</em> and <em>B</em> play.</p>
2721

28-
29-
3022
<p>Return the winner of the game if it exists (<em>A</em> or <em>B</em>), in case the game ends in a draw return &quot;Draw&quot;, if there are still movements to play return &quot;Pending&quot;.</p>
3123

32-
33-
3424
<p>You can assume that&nbsp;<code>moves</code> is&nbsp;<strong>valid</strong> (It follows the rules of Tic-Tac-Toe),&nbsp;the grid is initially empty and <em>A</em> will play <strong>first</strong>.</p>
3525

36-
37-
3826
<p>&nbsp;</p>
3927

4028
<p><strong>Example 1:</strong></p>
4129

42-
43-
4430
<pre>
4531

4632
<strong>Input:</strong> moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
@@ -57,12 +43,8 @@
5743

5844
</pre>
5945

60-
61-
6246
<p><strong>Example 2:</strong></p>
6347

64-
65-
6648
<pre>
6749

6850
<strong>Input:</strong> moves = [[0,0],[1,1],[0,1],[0,2],[1,0],[2,0]]
@@ -79,12 +61,8 @@
7961

8062
</pre>
8163

82-
83-
8464
<p><strong>Example 3:</strong></p>
8565

86-
87-
8866
<pre>
8967

9068
<strong>Input:</strong> moves = [[0,0],[1,1],[2,0],[1,0],[1,2],[2,1],[0,1],[0,2],[2,2]]
@@ -101,12 +79,8 @@
10179

10280
</pre>
10381

104-
105-
10682
<p><strong>Example 4:</strong></p>
10783

108-
109-
11084
<pre>
11185

11286
<strong>Input:</strong> moves = [[0,0],[1,1]]
@@ -123,14 +97,10 @@
12397

12498
</pre>
12599

126-
127-
128100
<p>&nbsp;</p>
129101

130102
<p><strong>Constraints:</strong></p>
131103

132-
133-
134104
<ul>
135105
<li><code>1 &lt;= moves.length &lt;= 9</code></li>
136106
<li><code>moves[i].length == 2</code></li>
@@ -146,13 +116,66 @@
146116
### **Python3**
147117

148118
```python
149-
119+
class Solution:
120+
def tictactoe(self, moves: List[List[int]]) -> str:
121+
n = len(moves)
122+
counter = [0] * 8
123+
for i in range(n - 1, -1, -2):
124+
row, col = moves[i][0], moves[i][1]
125+
counter[row] += 1
126+
counter[col + 3] += 1
127+
if row == col:
128+
counter[6] += 1
129+
if row + col == 2:
130+
counter[7] += 1
131+
if counter[row] == 3 or counter[col + 3] == 3 or counter[6] == 3 or counter[7] == 3:
132+
return "A" if (i % 2) == 0 else "B"
133+
return "Draw" if n == 9 else "Pending"
150134
```
151135

152136
### **Java**
153137

154138
```java
139+
class Solution {
140+
public String tictactoe(int[][] moves) {
141+
int n = moves.length;
142+
int[] counter = new int[8];
143+
for (int i = n - 1; i >= 0; i -= 2) {
144+
int row = moves[i][0], col = moves[i][1];
145+
++counter[row];
146+
++counter[col + 3];
147+
if (row == col) ++counter[6];
148+
if (row + col == 2) ++counter[7];
149+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
150+
return (i % 2) == 0 ? "A" : "B";
151+
}
152+
}
153+
return n == 9 ? "Draw" : "Pending";
154+
}
155+
}
156+
```
155157

158+
### **C++**
159+
160+
```cpp
161+
class Solution {
162+
public:
163+
string tictactoe(vector<vector<int>>& moves) {
164+
int n = moves.size();
165+
vector<int> counter(8, 0);
166+
for (int i = n - 1; i >= 0; i -= 2) {
167+
int row = moves[i][0], col = moves[i][1];
168+
++counter[row];
169+
++counter[col + 3];
170+
if (row == col) ++counter[6];
171+
if (row + col == 2) ++counter[7];
172+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
173+
return (i % 2 == 0) ? "A" : "B";
174+
}
175+
}
176+
return n == 9 ? "Draw" : "Pending";
177+
}
178+
};
156179
```
157180
158181
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
class Solution {
2-
bool checkWin(vector<vector<char>>& board, bool moveA) {
3-
char symbol = (moveA ? 'X' : 'O');
4-
for (int i = 0; i < 3; i++)
5-
if (board[i][0] == symbol && board[i][0] == board[i][1] && board[i][0] == board[i][2])
6-
return true;
7-
for (int i = 0; i < 3; i++)
8-
if (board[0][i] == symbol && board[0][i] == board[1][i] && board[0][i] == board[2][i])
9-
return true;
10-
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)
11-
return true;
12-
if (board[0][2] == symbol && board[1][1] == symbol && board[2][0] == symbol)
13-
return true;
14-
return false;
15-
}
162
public:
173
string tictactoe(vector<vector<int>>& moves) {
18-
vector<vector<char>> board(3, vector<char>(3, ' '));
19-
bool moveA = true;
20-
for (auto &v : moves) {
21-
board[v[0]][v[1]] = (moveA ? 'X' : 'O');
22-
if (checkWin(board, moveA))
23-
return (moveA ? "A" : "B");
24-
moveA = !moveA;
4+
int n = moves.size();
5+
vector<int> counter(8, 0);
6+
for (int i = n - 1; i >= 0; i -= 2) {
7+
int row = moves[i][0], col = moves[i][1];
8+
++counter[row];
9+
++counter[col + 3];
10+
if (row == col) ++counter[6];
11+
if (row + col == 2) ++counter[7];
12+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
13+
return (i % 2 == 0) ? "A" : "B";
14+
}
2515
}
26-
if (moves.size() == 9)
27-
return "Draw";
28-
return "Pending";
16+
return n == 9 ? "Draw" : "Pending";
2917
}
3018
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String tictactoe(int[][] moves) {
3+
int n = moves.length;
4+
int[] counter = new int[8];
5+
for (int i = n - 1; i >= 0; i -= 2) {
6+
int row = moves[i][0], col = moves[i][1];
7+
++counter[row];
8+
++counter[col + 3];
9+
if (row == col) ++counter[6];
10+
if (row + col == 2) ++counter[7];
11+
if (counter[row] == 3 || counter[col + 3] == 3 || counter[6] == 3 || counter[7] == 3) {
12+
return (i % 2) == 0 ? "A" : "B";
13+
}
14+
}
15+
return n == 9 ? "Draw" : "Pending";
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def tictactoe(self, moves: List[List[int]]) -> str:
3+
n = len(moves)
4+
counter = [0] * 8
5+
for i in range(n - 1, -1, -2):
6+
row, col = moves[i][0], moves[i][1]
7+
counter[row] += 1
8+
counter[col + 3] += 1
9+
if row == col:
10+
counter[6] += 1
11+
if row + col == 2:
12+
counter[7] += 1
13+
if counter[row] == 3 or counter[col + 3] == 3 or counter[6] == 3 or counter[7] == 3:
14+
return "A" if (i % 2) == 0 else "B"
15+
return "Draw" if n == 9 else "Pending"

0 commit comments

Comments
 (0)