Skip to content

Commit 0b3d24b

Browse files
refactor 348
1 parent 144e179 commit 0b3d24b

File tree

1 file changed

+6
-67
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+6
-67
lines changed

src/main/java/com/fishercoder/solutions/_348.java

Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,10 @@
11
package com.fishercoder.solutions;
22

3-
/**
4-
* 348. Design Tic-Tac-Toe
5-
*
6-
* Design a Tic-tac-toe game that is played between two players on a n x n grid.
7-
8-
You may assume the following rules:
9-
10-
A move is guaranteed to be valid and is placed on an empty block.
11-
Once a winning condition is reached, no more moves is allowed.
12-
A player who succeeds in placing n of their marks in a horizontal, vertical, or diagonal row wins the game.
13-
14-
Example:
15-
16-
Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.
17-
18-
TicTacToe toe = new TicTacToe(3);
19-
20-
toe.move(0, 0, 1); -> Returns 0 (no one wins)
21-
|X| | |
22-
| | | | // Player 1 makes a move at (0, 0).
23-
| | | |
24-
25-
toe.move(0, 2, 2); -> Returns 0 (no one wins)
26-
|X| |O|
27-
| | | | // Player 2 makes a move at (0, 2).
28-
| | | |
29-
30-
toe.move(2, 2, 1); -> Returns 0 (no one wins)
31-
|X| |O|
32-
| | | | // Player 1 makes a move at (2, 2).
33-
| | |X|
34-
35-
toe.move(1, 1, 2); -> Returns 0 (no one wins)
36-
|X| |O|
37-
| |O| | // Player 2 makes a move at (1, 1).
38-
| | |X|
39-
40-
toe.move(2, 0, 1); -> Returns 0 (no one wins)
41-
|X| |O|
42-
| |O| | // Player 1 makes a move at (2, 0).
43-
|X| |X|
44-
45-
toe.move(1, 0, 2); -> Returns 0 (no one wins)
46-
|X| |O|
47-
|O|O| | // Player 2 makes a move at (1, 0).
48-
|X| |X|
49-
50-
toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
51-
|X| |O|
52-
|O|O| | // Player 1 makes a move at (2, 1).
53-
|X|X|X|
54-
55-
Follow up:
56-
Could you do better than O(n2) per move() operation?
57-
58-
Hint:
59-
60-
Could you trade extra space such that move() operation can be done in O(1)?
61-
You need two arrays: int rows[n], int cols[n], plus two variables: diagonal, anti_diagonal.
62-
63-
*/
643
public class _348 {
654
public static class Solution1 {
665
/**
676
* credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand
68-
*
7+
* <p>
698
* Key: in order to win a TicTacToe, you must have the entire row or column, thus, we don't need
709
* to keep track of the entire n^2 board. We only need to keep a count for each row and column.
7110
* If at any time, a row or column matches the size of the board, then that player has won.
@@ -97,8 +36,8 @@ public TicTacToe(int n) {
9736
/**
9837
* Player {player} makes a move at ({row}, {col}).
9938
*
100-
* @param row The row of the board.
101-
* @param col The column of the board.
39+
* @param row The row of the board.
40+
* @param col The column of the board.
10241
* @param player The player, can be either 1 or 2.
10342
* @return The current winning condition, can be either: 0: No one wins. 1: Player 1 wins. 2:
10443
* Player 2 wins.
@@ -118,9 +57,9 @@ public int move(int row, int col, int player) {
11857
}
11958

12059
if (Math.abs(rows[row]) == size
121-
|| Math.abs(cols[col]) == size
122-
|| Math.abs(antidiagonal) == size
123-
|| Math.abs(diagonal) == size) {
60+
|| Math.abs(cols[col]) == size
61+
|| Math.abs(antidiagonal) == size
62+
|| Math.abs(diagonal) == size) {
12463
return player;
12564
}
12665

0 commit comments

Comments
 (0)