# simple c++ solution，beats 99%，9ms

• ``````bool isValidSudoku(vector<vector<char>>& board) {
bitset<9> nums;
//each row
for (size_t i = 0; i < board.size(); ++i) {
for (size_t j = 0; j < board[i].size(); ++j) {
if (isdigit(board[i][j])) {
if (nums.test(board[i][j] - '1')) {
return false;
} else {
nums.set(board[i][j] - '1');
}
}
}
nums.reset();
}

//each col
for (size_t j = 0; j < board[0].size(); ++j) {
for (size_t i = 0; i < board.size(); ++i) {
if (isdigit(board[i][j])) {
if (nums.test(board[i][j] - '1')) {
return false;
} else {
nums.set(board[i][j] - '1');
}
}
}
nums.reset();
}

//each block
for (size_t i = 0; i < 9; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t k = 0; k < 3; ++k) {
if (isdigit(board[i / 3 * 3 + j][i % 3 * 3 + k])) {
if (nums.test(board[i / 3 * 3 + j][i % 3 * 3 + k] - '1')) {
return false;
} else {
nums.set(board[i / 3 * 3 + j][i % 3 * 3 + k] - '1');
}
}
}
}
nums.reset();
}

return true;
}
``````

• @qibolee

can you please explain why you have

board[i / 3 * 3 + j][i % 3 * 3 + k]

in particular, why divide i by 3 and then multiple it by 3?

• @randy_wales
In C++, interger / integer = integer, so [0, 1, 2, 3, 4, 5, 6, 7, 8] will be [0, 0, 0, 3, 3, 3, 6, 6, 6]

• This post is deleted!

• @Chen_Wayne
seems you didn't read the description carefully

Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.