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.
1 Public classSolution {2 Public BooleanIsvalidsudoku (Char[] board) {3 for(inti=0; i<9; i++) {4 if(!isparticallyvalid (board,i,0,i,8))return false;5 if(!isparticallyvalid (Board,0,i,8,i))return false;6 }7 for(inti=0;i<3;i++){8 for(intj=0;j<3;j++){9 if(!isparticallyvalid (board,i*3,j*3,i*3+2,j*3+2))return false;Ten } One } A return true; - } - Private BooleanIsparticallyvalid (Char[] board,intX1,intY1,intX2,inty2) { theSet Singleset =NewHashSet (); - for(intI= X1; i<=x2; i++){ - for(intj=y1;j<=y2; J + +){ - if (board[i][j]!= '. ') if (!singleset.add (Board[i][j])) return false ;//If This set already contains the element, the call leaves the set unchanged and returns false. + } - } + return true; A } at -}
Https://leetcode.com/discuss/17990/sharing-my-easy-understand-java-solution-using-set
* Valid Sudoku