1
1
package com .fishercoder .solutions ;
2
2
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
- */
64
3
public class _348 {
65
4
public static class Solution1 {
66
5
/**
67
6
* credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand
68
- *
7
+ * <p>
69
8
* Key: in order to win a TicTacToe, you must have the entire row or column, thus, we don't need
70
9
* to keep track of the entire n^2 board. We only need to keep a count for each row and column.
71
10
* 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) {
97
36
/**
98
37
* Player {player} makes a move at ({row}, {col}).
99
38
*
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.
102
41
* @param player The player, can be either 1 or 2.
103
42
* @return The current winning condition, can be either: 0: No one wins. 1: Player 1 wins. 2:
104
43
* Player 2 wins.
@@ -118,9 +57,9 @@ public int move(int row, int col, int player) {
118
57
}
119
58
120
59
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 ) {
124
63
return player ;
125
64
}
126
65
0 commit comments