forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
34 lines (34 loc) · 947 Bytes
/
Solution.ts
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
function tictactoe(board: string[]): string {
const n = board.length;
const rows = Array(n).fill(0);
const cols = Array(n).fill(0);
let [dg, udg] = [0, 0];
let hasEmptyGrid = false;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < n; ++j) {
const c = board[i][j];
if (c === ' ') {
hasEmptyGrid = true;
continue;
}
const v = c === 'X' ? 1 : -1;
rows[i] += v;
cols[j] += v;
if (i === j) {
dg += v;
}
if (i + j === n - 1) {
udg += v;
}
if (
Math.abs(rows[i]) === n ||
Math.abs(cols[j]) === n ||
Math.abs(dg) === n ||
Math.abs(udg) === n
) {
return c;
}
}
}
return hasEmptyGrid ? 'Pending' : 'Draw';
}