Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
Discuss
#include#include using namespace std; class Solution { private: bool isExist(int &Input, int Number) { if (Input & (1 << Number)) { return true; } Input = Input | (1 << Number); return false; } public: bool isValidSudoku(vector > &board) { int X_value[9] = {0}; int Y_value[9] = {0}; int Inside[9] = {0}; for (int Index_y = 0; Index_y < 9; Index_y++) { vector &TmpRow = board[Index_y]; for (int Index_x = 0; Index_x < 9; Index_x++) { if (TmpRow[Index_x] == '.') { continue; } int TmpValue = TmpRow[Index_x] - '0'; // is valid in Index_x row if (isExist(X_value[Index_x], TmpValue)) { return false; } // is valid in Index_y col if (isExist(Y_value[Index_y], TmpValue)) { return false; } // is valid in 3*3 sub block if (isExist(Inside[Index_x / 3 + Index_y / 3 * 3] , TmpValue)) { return false; } } } return true; } };