/ **
* IS Sudoku the Determine IF A Valid, ACCORDING to:. - Sudoku Puzzles of The Rules
'.'. * Could Board of The Sudoku Partially Filled BE, empty cells are Filled with WHERE The Character
* Partially Filled Sudoku Which IS A Valid.
*
* determined according to the following rules only if the number of valid:
* number plate may only partially filled, empty cells wherein fill characters. "." Empty cell is used. Instead.
* A number of partially filled alone is effective.
* /
Analysis:
* The purpose of this question means that this number alone is valid, returns to true, false.
* There are three cases is not true, returns false:
* The first: each line cycle, if the same number appeared not established, encounter . skip
* the second: each column cycle, if the same number appeared not established encountered skip.
* third: the cycle every little squares, if the same number appears not established, the case to. skip (this cycle need to look at ways the law)
java.util.HashSet Import; Import java.util.Set; / ** * the Determine IF IS A Sudoku! Valid, ACCORDING to:. Sudoku Puzzles - at The Rules * at The Sudoku Board could BE Partially Filled, the WHERE Filled with empty cells are at The '.'. character * partially filled Sudoku which IS A valid. * * only determines the number is valid according to the following rules: . "" * number plate may only partially filled, empty cells wherein fill characters. Empty cell is used. Instead. * A number of partially filled alone is effective. * * Resolution: * The purpose of this question means that this number alone is valid, returns to true, false. * There are three cases is not true, returns false: * The first: each line cycle, if the same number appeared not established, encounter skip. * the second: each column cycle, if the same number appeared not established encountered skip. * third: the cycle every little squares, if the same number appeared not established encountered. skip (this cycle need to look at ways the law) * / public class Main40 { public static void main(String[] args) { char[][] board = { {'.','.','4','.','.','.','6','3','.'}, {'.','.','.','.','.','.','.','.','.'}, {'5','.','.','.','.','.','.','9','.'}, {'.','.','.','5','6','.','.','.','.'}, {'4','.','3','.','.','.','.','.','.'}, {'.','.','.','7','.','.','.','.','.'}, {'.','.','.','1','.','.','.','.','.'}, {'.','.','.','.','.','.','.','.','.'}, {'.','.','.','.','.','.','.','.','.'} }; System.out.println(Main40.isValidSudoku(board)); } public static boolean isValidSudoku(char[][] board) { Set<Character> set = new HashSet<>(); // 第一种情况: for (int i=0;i<board.length;i++) { for (int j=0;j<board[i].length;j++) { if (board[i][j] == '.') { continue; } if (!set.contains(board[i][j])) { set.add(board[i][j]); }else{ return false; } } set.clear(); } // 第二种情况: for (int j=0;j<board[0].length;j++) { for (int i=0;i<board.length;i++) { if (board[i][j] == '.') { continue; } if (!set.contains(board[i][j])) { set.add(board[i][j]); }else{ return false; } } set.clear(); } // 第三种情况: for (int k=0;k<board.length;k++) { for (int i=k/3*3;i<k/3*3+3;i++) { for (int j=k%3*3;j<k%3*3+3;j++) { if (board[i][j] == '.') { continue; } if (!set.contains(board[i][j])) { set.add(board[i][j]); }else{ return false; } } } set.clear(); } return true; } }