Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
design-tic-tac-toe.cpp 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-05-06 19:26 +08:00 . Update design-tic-tac-toe.cpp
// Time: O(1), per move.
// Space: O(n^2)
class TicTacToe {
public:
/** Initialize your data structure here. */
TicTacToe(int n) : rows_(n, {0, 0}), cols_(n, {0, 0}),
diagonal_(2, 0), anti_diagonal_(2, 0) {
}
/** Player {player} makes a move at ({row}, {col}).
@param row The row of the board.
@param col The column of the board.
@param player The player, can be either 1 or 2.
@return The current winning condition, can be either:
0: No one wins.
1: Player 1 wins.
2: Player 2 wins. */
int move(int row, int col, int player) {
const auto i = player - 1;
++rows_[row][i], ++cols_[col][i];
if (row == col) {
++diagonal_[i];
}
if (col == rows_.size() - row - 1) {
++anti_diagonal_[i];
}
if (rows_[row][i] == rows_.size() ||
cols_[col][i] == cols_.size() ||
diagonal_[i] == rows_.size() ||
anti_diagonal_[i] == cols_.size()) {
return player;
}
return 0;
}
private:
vector<vector<int>> rows_, cols_;
vector<int> diagonal_, anti_diagonal_;
};
/**
* Your TicTacToe object will be instantiated and called as such:
* TicTacToe obj = new TicTacToe(n);
* int param_1 = obj.move(row,col,player);
*/
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助