分类讨论芜湖~~~
谢谢官方题解~~~
class Solution {
public boolean validTicTacToe(String[] board) {
int xCount = 0;
int oCount = 0;
for (String row : board) {
for (char c : row.toCharArray()) {
xCount = (c == 'X') ? (xCount + 1) : xCount;
oCount = (c == 'O') ? (oCount + 1) : oCount;
}
}
if (oCount != xCount && oCount != xCount - 1) {
return false;
}
if (win(board, 'X') && oCount != xCount - 1) {
return false;
}
if (win(board, 'O') && oCount != xCount) {
return false;
}
return true;
}
public boolean win(String[] board, char p) {
for (int i = 0; i < 3; ++i) {
if (p == board[0].charAt(i) && p == board[1].charAt(i) && p == board[2].charAt(i)) {
return true;
}
if (p == board[i].charAt(0) && p == board[i].charAt(1) && p == board[i].charAt(2)) {
return true;
}
}
if (p == board[0].charAt(0) && p == board[1].charAt(1) && p == board[2].charAt(2)) {
return true;
}
if (p == board[0].charAt(2) && p == board[1].charAt(1) && p == board[2].charAt(0)) {
return true;
}
return false;
}
}