Skip to content

Commit 5b1d7f4

Browse files
committed
feat: add solutions to lc problem: No.0348. Design Tic-Tac-Toe
1 parent 06628e3 commit 5b1d7f4

File tree

4 files changed

+224
-6
lines changed

4 files changed

+224
-6
lines changed

solution/0300-0399/0348.Design Tic-Tac-Toe/README.md

+76-3
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,100 @@ toe.move(2, 1, 1); -> 函数返回 1 (此时,玩家 1 赢得了该场比赛
6565
<p><strong>进阶:</strong><br>
6666
您有没有可能将每一步的&nbsp;<code>move()</code>&nbsp;操作优化到比&nbsp;O(<em>n</em><sup>2</sup>) 更快吗?</p>
6767

68-
6968
## 解法
7069

7170
<!-- 这里可写通用的实现逻辑 -->
7271

72+
思路同[1275. 找出井字棋的获胜者](solution/1200-1299/1275.Find%20Winner%20on%20a%20Tic%20Tac%20Toe%20Game/README)
73+
7374
<!-- tabs:start -->
7475

7576
### **Python3**
7677

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

7980
```python
80-
81+
class TicTacToe:
82+
83+
def __init__(self, n: int):
84+
"""
85+
Initialize your data structure here.
86+
"""
87+
self.n = n
88+
self.counter = [[0] * ((n << 1) + 2) for _ in range(2)]
89+
90+
def move(self, row: int, col: int, player: int) -> int:
91+
"""
92+
Player {player} makes a move at ({row}, {col}).
93+
@param row The row of the board.
94+
@param col The column of the board.
95+
@param player The player, can be either 1 or 2.
96+
@return The current winning condition, can be either:
97+
0: No one wins.
98+
1: Player 1 wins.
99+
2: Player 2 wins.
100+
"""
101+
n = self.n
102+
self.counter[player - 1][row] += 1
103+
self.counter[player - 1][col + n] += 1
104+
if row == col:
105+
self.counter[player - 1][n << 1] += 1
106+
if row + col == n - 1:
107+
self.counter[player - 1][(n << 1) + 1] += 1
108+
if self.counter[player - 1][row] == n or self.counter[player - 1][col + n] == n or self.counter[player - 1][n << 1] == n or self.counter[player - 1][(n << 1) + 1] == n:
109+
return player
110+
return 0
111+
112+
113+
# Your TicTacToe object will be instantiated and called as such:
114+
# obj = TicTacToe(n)
115+
# param_1 = obj.move(row,col,player)
81116
```
82117

83118
### **Java**
84119

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

87122
```java
88-
123+
class TicTacToe {
124+
private int n;
125+
private int[][] counter;
126+
127+
/** Initialize your data structure here. */
128+
public TicTacToe(int n) {
129+
counter = new int[2][(n << 1) + 2];
130+
this.n = n;
131+
}
132+
133+
/** Player {player} makes a move at ({row}, {col}).
134+
@param row The row of the board.
135+
@param col The column of the board.
136+
@param player The player, can be either 1 or 2.
137+
@return The current winning condition, can be either:
138+
0: No one wins.
139+
1: Player 1 wins.
140+
2: Player 2 wins. */
141+
public int move(int row, int col, int player) {
142+
counter[player - 1][row] += 1;
143+
counter[player - 1][col + n] += 1;
144+
if (row == col) {
145+
counter[player - 1][n << 1] += 1;
146+
}
147+
if (row + col == n - 1) {
148+
counter[player - 1][(n << 1) + 1] += 1;
149+
}
150+
if (counter[player - 1][row] == n || counter[player - 1][col + n] == n || counter[player - 1][n << 1] == n || counter[player - 1][(n << 1) + 1] == n) {
151+
return player;
152+
}
153+
return 0;
154+
}
155+
}
156+
157+
/**
158+
* Your TicTacToe object will be instantiated and called as such:
159+
* TicTacToe obj = new TicTacToe(n);
160+
* int param_1 = obj.move(row,col,player);
161+
*/
89162
```
90163

91164
### **...**

solution/0300-0399/0348.Design Tic-Tac-Toe/README_EN.md

+74-3
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,92 @@ ticTacToe.move(2, 1, 1); // return 1&nbsp;(player 1 wins)
8282
<li>At most <code>n<sup>2</sup></code> calls will be made to <code>move</code>.</li>
8383
</ul>
8484

85-
8685
## Solutions
8786

8887
<!-- tabs:start -->
8988

9089
### **Python3**
9190

9291
```python
93-
92+
class TicTacToe:
93+
94+
def __init__(self, n: int):
95+
"""
96+
Initialize your data structure here.
97+
"""
98+
self.n = n
99+
self.counter = [[0] * ((n << 1) + 2) for _ in range(2)]
100+
101+
def move(self, row: int, col: int, player: int) -> int:
102+
"""
103+
Player {player} makes a move at ({row}, {col}).
104+
@param row The row of the board.
105+
@param col The column of the board.
106+
@param player The player, can be either 1 or 2.
107+
@return The current winning condition, can be either:
108+
0: No one wins.
109+
1: Player 1 wins.
110+
2: Player 2 wins.
111+
"""
112+
n = self.n
113+
self.counter[player - 1][row] += 1
114+
self.counter[player - 1][col + n] += 1
115+
if row == col:
116+
self.counter[player - 1][n << 1] += 1
117+
if row + col == n - 1:
118+
self.counter[player - 1][(n << 1) + 1] += 1
119+
if self.counter[player - 1][row] == n or self.counter[player - 1][col + n] == n or self.counter[player - 1][n << 1] == n or self.counter[player - 1][(n << 1) + 1] == n:
120+
return player
121+
return 0
122+
123+
124+
# Your TicTacToe object will be instantiated and called as such:
125+
# obj = TicTacToe(n)
126+
# param_1 = obj.move(row,col,player)
94127
```
95128

96129
### **Java**
97130

98131
```java
99-
132+
class TicTacToe {
133+
private int n;
134+
private int[][] counter;
135+
136+
/** Initialize your data structure here. */
137+
public TicTacToe(int n) {
138+
counter = new int[2][(n << 1) + 2];
139+
this.n = n;
140+
}
141+
142+
/** Player {player} makes a move at ({row}, {col}).
143+
@param row The row of the board.
144+
@param col The column of the board.
145+
@param player The player, can be either 1 or 2.
146+
@return The current winning condition, can be either:
147+
0: No one wins.
148+
1: Player 1 wins.
149+
2: Player 2 wins. */
150+
public int move(int row, int col, int player) {
151+
counter[player - 1][row] += 1;
152+
counter[player - 1][col + n] += 1;
153+
if (row == col) {
154+
counter[player - 1][n << 1] += 1;
155+
}
156+
if (row + col == n - 1) {
157+
counter[player - 1][(n << 1) + 1] += 1;
158+
}
159+
if (counter[player - 1][row] == n || counter[player - 1][col + n] == n || counter[player - 1][n << 1] == n || counter[player - 1][(n << 1) + 1] == n) {
160+
return player;
161+
}
162+
return 0;
163+
}
164+
}
165+
166+
/**
167+
* Your TicTacToe object will be instantiated and called as such:
168+
* TicTacToe obj = new TicTacToe(n);
169+
* int param_1 = obj.move(row,col,player);
170+
*/
100171
```
101172

102173
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class TicTacToe {
2+
private int n;
3+
private int[][] counter;
4+
5+
/** Initialize your data structure here. */
6+
public TicTacToe(int n) {
7+
counter = new int[2][(n << 1) + 2];
8+
this.n = n;
9+
}
10+
11+
/** Player {player} makes a move at ({row}, {col}).
12+
@param row The row of the board.
13+
@param col The column of the board.
14+
@param player The player, can be either 1 or 2.
15+
@return The current winning condition, can be either:
16+
0: No one wins.
17+
1: Player 1 wins.
18+
2: Player 2 wins. */
19+
public int move(int row, int col, int player) {
20+
counter[player - 1][row] += 1;
21+
counter[player - 1][col + n] += 1;
22+
if (row == col) {
23+
counter[player - 1][n << 1] += 1;
24+
}
25+
if (row + col == n - 1) {
26+
counter[player - 1][(n << 1) + 1] += 1;
27+
}
28+
if (counter[player - 1][row] == n || counter[player - 1][col + n] == n || counter[player - 1][n << 1] == n || counter[player - 1][(n << 1) + 1] == n) {
29+
return player;
30+
}
31+
return 0;
32+
}
33+
}
34+
35+
/**
36+
* Your TicTacToe object will be instantiated and called as such:
37+
* TicTacToe obj = new TicTacToe(n);
38+
* int param_1 = obj.move(row,col,player);
39+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class TicTacToe:
2+
3+
def __init__(self, n: int):
4+
"""
5+
Initialize your data structure here.
6+
"""
7+
self.n = n
8+
self.counter = [[0] * ((n << 1) + 2) for _ in range(2)]
9+
10+
def move(self, row: int, col: int, player: int) -> int:
11+
"""
12+
Player {player} makes a move at ({row}, {col}).
13+
@param row The row of the board.
14+
@param col The column of the board.
15+
@param player The player, can be either 1 or 2.
16+
@return The current winning condition, can be either:
17+
0: No one wins.
18+
1: Player 1 wins.
19+
2: Player 2 wins.
20+
"""
21+
n = self.n
22+
self.counter[player - 1][row] += 1
23+
self.counter[player - 1][col + n] += 1
24+
if row == col:
25+
self.counter[player - 1][n << 1] += 1
26+
if row + col == n - 1:
27+
self.counter[player - 1][(n << 1) + 1] += 1
28+
if self.counter[player - 1][row] == n or self.counter[player - 1][col + n] == n or self.counter[player - 1][n << 1] == n or self.counter[player - 1][(n << 1) + 1] == n:
29+
return player
30+
return 0
31+
32+
33+
# Your TicTacToe object will be instantiated and called as such:
34+
# obj = TicTacToe(n)
35+
# param_1 = obj.move(row,col,player)

0 commit comments

Comments
 (0)