Determine if a Sudoku is valid, according to: Sudoku puzzles-the rules.
The sudoku board cocould 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.
You should know the rules when you have played jiugongge (you can try it if you have never done it)
(1) 1 ~ 9 each appears once
(2) 1 ~ 9 each appears once
(3) Each small 3 gongge, 1 ~ 9 each appears once
class Solution {public: bool isValidRow(vector<vector<char> >& board){ for(int row = 0; row < 9; ++ row){ vector<int> cnt(10,0); for(int col = 0; col < 9; ++ col){ char item = board[row][col]; if(item != ‘.‘){ if(cnt[item-‘0‘]!=0) return false; else cnt[item-‘0‘]++; } } } return true; } bool isValidCol(vector<vector<char> >& board ){ for(int col = 0; col < 9; ++ col){ vector<int> cnt(10,0); for(int row = 0; row < 9; ++ row){ char item = board[row][col]; if(item != ‘.‘){ if(cnt[item-‘0‘]!=0) return false; else cnt[item-‘0‘]++; } } } return true; } bool isValidBox(vector<vector<char> >& board){ for(int i = 0 ; i < 3; ++ i){ for(int j = 0 ; j < 3; ++ j){ vector<int> cnt(10,0); for(int row = 3*i;row < 3*i+3; ++row){ for(int col = 3*j; col < 3*j+3; ++col){ char item = board[row][col]; if(item != ‘.‘){ if(cnt[item-‘0‘]!=0) return false; else cnt[item-‘0‘]++; } } } } } return true; } bool isValidSudoku(vector<vector<char> > &board) { return isValidRow(board)&&isValidCol(board)&&isValidBox(board); }};