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.
Public classSolution { Public BooleanIsvalidsudoku (Char[] board) { //The idea: first each row of each column to determine whether there is a number of repetitions, the problem with the help of ArrayList, pay attention to Clear,contains,add and other methods, when the board element is not '. ' Only when the judgment is made.//judge the rows and columns to determine the block of each 3*3. Note the way of traversal, four for loops intlen=board.length; List<Character> row=NewArraylist<character>(); List<Character> col=NewArraylist<character>(); for(inti=0;i<len;i++) {row.clear (); Col.clear (); for(intj=0;j<len;j++){ CharR=Board[i][j]; if(r!= '. ')){ if(Row.contains (R))return false; Else{Row.add (R); } } CharC=Board[j][i]; if(c!= '. ')){ if(Col.contains (c))return false; Else{Col.add (c); } }}} ArrayList<Character> block=NewArraylist<character>(); for(intK=0;K<LEN;K=K+3) {//K represents the row of the block, and S represents the column of the block. Each block is then traversed for(intS=0;s<len;s=s+3) {block.clear (); for(inti=0;i<3;i++){ for(intj=0;j<3;j++){ Charb=board[i+k][j+S]; if(b!= '. ')){ if(Block.contains (b))return false; Else{Block.add (b); } } } } } } return true; }}
[Leedcode 36] Valid Sudoku