class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
//row[m][n]代表第m+1行是否已使用数字n+1
//col[m][n]代表第m+1列是否已使用数字n+1
//sub_33[m][n]将格子分成了9个小的3x3,m代表第几个3x3小格子,n代表n+1是否已使用
int row[9][9] ={0},col[9][9]={0},sub_33[9][9]={0};
for(int i = 0;i < board.size();i++)
for(int j = 0;j < board[i].size();j++){
if(board[i][j] != '.')
{
int num = board[i][j] - '0' -1,k = i / 3 * 3 + j/3;
if(row[i][num] || col[j][num] || sub_33[k][num])
return false;
row[i][num] = col[j][num] = sub_33[k][num] = 1;
}
}
return true;
}
};
运行结果:
Runtime: 16 ms, faster than 99.91% of C++ online submissions for Valid Sudoku.
Memory Usage: 18.3 MB, less than 83.66% of C++ online submissions for Valid Sudoku.