来源:Leetcode
原帖:http://oj.leetcode.com/problems/valid-sudoku/
题目:
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules (http://sudoku.com.au/TheRules.aspx). The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
代码:
原帖:http://oj.leetcode.com/problems/valid-sudoku/
题目:
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules (http://sudoku.com.au/TheRules.aspx). The Sudoku board could be partially filled, where empty cells are filled with the character '.'.
代码:
class Solution {
public:
bool isValidSudoku(vector<vector<char>> &board) {
const int N = 9;
vector<int> col(N, 0), box(N, 0);
for (int i = 0; i < N; ++i) { // row traverse
int row = 0; // update row bit
for (int j = 0; j < N; ++j) { // col traverse
if (board[i][j] == '.') continue;
int bit = 1 << (board[i][j] - '1');
int box_index = i/3*3 + j/3;
if ((row & bit) || (col[j] & bit) || (box[box_index] & bit)) {
return false;
}
row |= bit;
col[j] |= bit;
box[box_index] |= bit;
}
}
return true;
}
};
No comments:
Post a Comment