Topic links
Title Requirements:
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.
The solution of this problem is not quite ingenious, so the method used in the following procedure is brute force (violent cracking):
1 classSolution {2 Public:3 intChartoint (Charc)4 {5 CharStr[] = {'1','2','3','4','5','6','7','8','9'};6 intIntstr[] = {1,2,3,4,5,6,7,8,9};7 for(inti =0; I <9; i++)8 {9 if(c = =Str[i])Ten returnIntstr[i]; One } A return-1; - } - the BOOLIsvalidsudoku (vector<vector<Char>>&Board) { -unordered_map<int,int> hashMap; - for(inti =1; I <Ten; i++) -Hashmap[i] =0; + //row by Row - for(inti =0; I <9; i++) + { A for(intK =0; K <Ten; k++) atHASHMAP[K] =0; - for(intj =0; J <9; J + +) - { - intTMP =Chartoint (Board[i][j]); - if(tmp! =-1) - { inhashmap[tmp]++; - if(Hashmap[tmp] >1) to return false; + } - } the } * //column by Column $ for(intj =0; J <9; J + +)Panax Notoginseng { - for(intK =1; K <Ten; k++) theHASHMAP[K] =0; + for(inti =0; I <9; i++) A { the intTMP =Chartoint (Board[i][j]); + if(tmp! =-1) - { $hashmap[tmp]++; $ if(Hashmap[tmp] >1) - return false; - } the } - }Wuyi //3*3 boxes by 3*3 boxes the for(inti =0; I <9; i + =3) - { Wu for(intj =0; J <9; J + =3) - { About for(intK =0; K <Ten; k++) $HASHMAP[K] =0; - for(intm = i; M < i +3; m++) - for(intn = j; N < J +3; n++) - { A intTMP =Chartoint (Board[m][n]); + if(tmp! =-1) the { -hashmap[tmp]++; $ if(Hashmap[tmp] >1) the return false; the } the } the } - } in the return true; the } About};
Leetcode's "hash list": Valid Sudoku