Skip to content

Commit 69ffc4f

Browse files
refactor 348
1 parent 7282931 commit 69ffc4f

File tree

2 files changed

+83
-92
lines changed

2 files changed

+83
-92
lines changed

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

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -62,76 +62,70 @@ Could you trade extra space such that move() operation can be done in O(1)?
6262
6363
*/
6464
public class _348 {
65-
/**
66-
* credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand
67-
*
68-
* Key: in order to win a TicTacToe, you must have the entire row or column,
69-
* thus, we don't need to keep track of the entire n^2 board.
70-
* We only need to keep a count for each row and column.
71-
* If at any time, a row or column matches the size of the board, then that player has won.*/
72-
public static class TicTacToe {
73-
74-
private int diagonal;
65+
public static class Solution1 {
7566
/**
76-
* This is diagonal:
77-
* |X| | |
78-
* | |X| |
79-
* | | |X|
80-
* So, its condition is always like this: if (row == col)
81-
*/
82-
83-
private int antidiagonal;
84-
/**
85-
* This is antidiagonal:
86-
* | | |X|
87-
* | |X| |
88-
* |X| | |
89-
* So, its condition is always like this: if (col == size - row - 1)
90-
*/
91-
private int[] rows;
92-
private int[] cols;
93-
94-
/**
95-
* Initialize your data structure here.
96-
*/
97-
public TicTacToe(int n) {
98-
rows = new int[n];
99-
cols = new int[n];
100-
}
101-
102-
/**
103-
* Player {player} makes a move at ({row}, {col}).
67+
* credit: https://discuss.leetcode.com/topic/44548/java-o-1-solution-easy-to-understand
10468
*
105-
* @param row The row of the board.
106-
* @param col The column of the board.
107-
* @param player The player, can be either 1 or 2.
108-
* @return The current winning condition, can be either:
109-
* 0: No one wins.
110-
* 1: Player 1 wins.
111-
* 2: Player 2 wins.
69+
* Key: in order to win a TicTacToe, you must have the entire row or column, thus, we don't need
70+
* to keep track of the entire n^2 board. We only need to keep a count for each row and column.
71+
* If at any time, a row or column matches the size of the board, then that player has won.
11272
*/
113-
public int move(int row, int col, int player) {
114-
int toAdd = player == 1 ? 1 : -1;
115-
116-
rows[row] += toAdd;
117-
cols[col] += toAdd;
118-
int size = rows.length;
119-
120-
if (row == col) {
121-
diagonal += toAdd;
122-
}
123-
if (col == (size - row - 1)) {
124-
antidiagonal += toAdd;
73+
public static class TicTacToe {
74+
75+
private int diagonal;
76+
/**
77+
* This is diagonal: |X| | | | |X| | | | |X| So, its condition is always like this: if (row ==
78+
* col)
79+
*/
80+
81+
private int antidiagonal;
82+
/**
83+
* This is antidiagonal: | | |X| | |X| | |X| | | So, its condition is always like this: if
84+
* (col == size - row - 1)
85+
*/
86+
private int[] rows;
87+
private int[] cols;
88+
89+
/**
90+
* Initialize your data structure here.
91+
*/
92+
public TicTacToe(int n) {
93+
rows = new int[n];
94+
cols = new int[n];
12595
}
12696

127-
if (Math.abs(rows[row]) == size
97+
/**
98+
* Player {player} makes a move at ({row}, {col}).
99+
*
100+
* @param row The row of the board.
101+
* @param col The column of the board.
102+
* @param player The player, can be either 1 or 2.
103+
* @return The current winning condition, can be either: 0: No one wins. 1: Player 1 wins. 2:
104+
* Player 2 wins.
105+
*/
106+
public int move(int row, int col, int player) {
107+
int toAdd = player == 1 ? 1 : -1;
108+
109+
rows[row] += toAdd;
110+
cols[col] += toAdd;
111+
int size = rows.length;
112+
113+
if (row == col) {
114+
diagonal += toAdd;
115+
}
116+
if (col == (size - row - 1)) {
117+
antidiagonal += toAdd;
118+
}
119+
120+
if (Math.abs(rows[row]) == size
128121
|| Math.abs(cols[col]) == size
129122
|| Math.abs(antidiagonal) == size
130123
|| Math.abs(diagonal) == size) {
131-
return player;
132-
}
124+
return player;
125+
}
133126

134-
return 0;
127+
return 0;
128+
}
135129
}
136130
}
137131
}

src/test/java/com/fishercoder/_348Test.java

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,35 @@
55

66
import static org.junit.Assert.assertEquals;
77

8-
/**
9-
* Created by fishercoder on 5/13/17.
10-
*/
118
public class _348Test {
12-
@Test
13-
public void test1() {
14-
int n = 3;
15-
_348.TicTacToe ticTacToe = new _348.TicTacToe(n);
16-
assertEquals(0, ticTacToe.move(0, 0, 1));
17-
assertEquals(0, ticTacToe.move(0, 2, 2));
18-
assertEquals(0, ticTacToe.move(2, 2, 1));
19-
assertEquals(0, ticTacToe.move(1, 1, 2));
20-
assertEquals(0, ticTacToe.move(2, 0, 1));
21-
assertEquals(0, ticTacToe.move(1, 0, 2));
22-
assertEquals(1, ticTacToe.move(2, 1, 1));
23-
}
9+
@Test
10+
public void test1() {
11+
int n = 3;
12+
_348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n);
13+
assertEquals(0, ticTacToe.move(0, 0, 1));
14+
assertEquals(0, ticTacToe.move(0, 2, 2));
15+
assertEquals(0, ticTacToe.move(2, 2, 1));
16+
assertEquals(0, ticTacToe.move(1, 1, 2));
17+
assertEquals(0, ticTacToe.move(2, 0, 1));
18+
assertEquals(0, ticTacToe.move(1, 0, 2));
19+
assertEquals(1, ticTacToe.move(2, 1, 1));
20+
}
2421

25-
@Test
26-
public void test2() {
27-
int n = 3;
28-
_348.TicTacToe ticTacToe = new _348.TicTacToe(n);
29-
assertEquals(0, ticTacToe.move(0, 0, 1));
30-
assertEquals(0, ticTacToe.move(1, 1, 1));
31-
assertEquals(1, ticTacToe.move(2, 2, 1));
32-
}
22+
@Test
23+
public void test2() {
24+
int n = 3;
25+
_348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n);
26+
assertEquals(0, ticTacToe.move(0, 0, 1));
27+
assertEquals(0, ticTacToe.move(1, 1, 1));
28+
assertEquals(1, ticTacToe.move(2, 2, 1));
29+
}
3330

34-
@Test
35-
public void test3() {
36-
int n = 3;
37-
_348.TicTacToe ticTacToe = new _348.TicTacToe(n);
38-
assertEquals(0, ticTacToe.move(0, 2, 2));
39-
assertEquals(0, ticTacToe.move(1, 1, 2));
40-
assertEquals(2, ticTacToe.move(2, 0, 2));
41-
}
31+
@Test
32+
public void test3() {
33+
int n = 3;
34+
_348.Solution1.TicTacToe ticTacToe = new _348.Solution1.TicTacToe(n);
35+
assertEquals(0, ticTacToe.move(0, 2, 2));
36+
assertEquals(0, ticTacToe.move(1, 1, 2));
37+
assertEquals(2, ticTacToe.move(2, 0, 2));
38+
}
4239
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy