Title Description: (link)
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is 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.
Problem Solving Ideas:
Verify the rows First, then verify the columns, and finally verify the small squares!
1 classSolution {2 Public:3 BOOLIsvalidsudoku (vector<vector<Char>>&Board) {4 BOOLused[9];5 6 for(inti =0; I <9; ++i) {7memset (Used,false,sizeof(used));8 for(intj =0; J <9; ++j) {9 if(!Isvaild (Board[i][j], used)) {Ten return false; One } A } - -memset (Used,false,sizeof(used)); the for(intj =0; J <9; ++j) { - if(!Isvaild (Board[j][i], used)) { - return false; - } + } - } + A for(intR =0; R <3; ++r) { at for(intc =0; C <3; ++c) { -memset (Used,false,sizeof(used)); - - for(inti = R *3; I <3* R +3; ++i) { - for(intj = c *3; J <3* C +3; ++j) { - if(!Isvaild (Board[i][j], used)) { in return false; - } to } + } - } the } * $ return true;Panax Notoginseng } - Private: the BOOLIsvaild (CharCBOOL*used) { + if(c = ='.') { A return true; the } + - if(Used[c-'1']) { $ return false; $ } - -Used[c-'1'] =true; the return true; - }Wuyi};
[Leetcode] Valid Sudoku