forked from szl0072/Leetcode-Solution-Code
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDesignTicTacToe.java
88 lines (72 loc) · 2.05 KB
/
DesignTicTacToe.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package leetcode;
/**
* Project Name : Leetcode
* Package Name : leetcode
* File Name : DesignTicTacToe
* Creator : Edward
* Date : Jan, 2018
* Description : 348. Design Tic-Tac-Toe
*/
public class DesignTicTacToe {
/**
* Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board.
TicTacToe toe = new TicTacToe(3);
toe.move(0, 0, 1); -> Returns 0 (no one wins)
|X| | |
| | | | // Player 1 makes a move at (0, 0).
| | | |
toe.move(0, 2, 2); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 2 makes a move at (0, 2).
| | | |
toe.move(2, 2, 1); -> Returns 0 (no one wins)
|X| |O|
| | | | // Player 1 makes a move at (2, 2).
| | |X|
toe.move(1, 1, 2); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 2 makes a move at (1, 1).
| | |X|
toe.move(2, 0, 1); -> Returns 0 (no one wins)
|X| |O|
| |O| | // Player 1 makes a move at (2, 0).
|X| |X|
toe.move(1, 0, 2); -> Returns 0 (no one wins)
|X| |O|
|O|O| | // Player 2 makes a move at (1, 0).
|X| |X|
toe.move(2, 1, 1); -> Returns 1 (player 1 wins)
|X| |O|
|O|O| | // Player 1 makes a move at (2, 1).
|X|X|X|
cols[1,-2,3]
* @param n
*/
private int[] rows;
private int[] cols;
private int diagonal;
private int antiDiagonal;
private int size;
public DesignTicTacToe(int n) {
rows = new int[n];
cols = new int[n];
this.size = n;
}
//time : O(1)
public int move(int row, int col, int player) {
int toAdd = player == 1 ? 1 : -1;
rows[row] += toAdd;
cols[col] += toAdd;
if (row == col) {
diagonal += toAdd;
}
if (col == (cols.length - row - 1)) {
antiDiagonal += toAdd;
}
if (Math.abs(rows[row]) == size || Math.abs(cols[col]) == size
|| Math.abs(diagonal) == size || Math.abs(antiDiagonal) == size) {
return player;
}
return 0;
}
}