|
| 1 | +packageeasy; |
| 2 | + |
| 3 | +publicclassValidSudoku { |
| 4 | + |
| 5 | +// this is my original solution, pretty straightforward, but lengthy, there's a very concise |
| 6 | +// version: https://discuss.leetcode.com/topic/9748/shared-my-concise-java-code, it uses |
| 7 | +//three HashSets in each loop, pretty cool! |
| 8 | +publicbooleanisValidSudoku(char[][]board) { |
| 9 | +for (inti =0;i <9;i++) { |
| 10 | +if (!isValidRow(board,i)) |
| 11 | +returnfalse; |
| 12 | + } |
| 13 | + |
| 14 | +for (intj =0;j <9;j++) { |
| 15 | +if (!isValidCol(board,j)) |
| 16 | +returnfalse; |
| 17 | + } |
| 18 | + |
| 19 | +for (inti =0;i <7;i =i +3) { |
| 20 | +for (intj =0;j <7;j =j +3) { |
| 21 | +if (!isValidSquare(board,i,j)) |
| 22 | +returnfalse; |
| 23 | + } |
| 24 | + } |
| 25 | +returntrue; |
| 26 | + } |
| 27 | + |
| 28 | +booleanisValidRow(char[][]board,introw) { |
| 29 | +int[]nums =newint[9]; |
| 30 | +for (inti =0;i <9;i++) { |
| 31 | +nums[i] =1; |
| 32 | + } |
| 33 | +for (intj =0;j <9;j++) { |
| 34 | +if (board[row][j] !='.') |
| 35 | +nums[Character.getNumericValue(board[row][j]) -1]--; |
| 36 | + } |
| 37 | +for (inti :nums) { |
| 38 | +if (i <0) |
| 39 | +returnfalse; |
| 40 | + } |
| 41 | +returntrue; |
| 42 | + } |
| 43 | + |
| 44 | +booleanisValidCol(char[][]board,intcol) { |
| 45 | +int[]nums =newint[9]; |
| 46 | +for (inti =0;i <9;i++) { |
| 47 | +nums[i] =1; |
| 48 | + } |
| 49 | +for (inti =0;i <9;i++) { |
| 50 | +if (board[i][col] !='.') |
| 51 | +nums[Character.getNumericValue(board[i][col]) -1]--; |
| 52 | + } |
| 53 | +for (inti :nums) { |
| 54 | +if (i <0) |
| 55 | +returnfalse; |
| 56 | + } |
| 57 | +returntrue; |
| 58 | + } |
| 59 | + |
| 60 | +booleanisValidSquare(char[][]board,introw,intcol) { |
| 61 | +int[]nums =newint[9]; |
| 62 | +for (inti =0;i <9;i++) { |
| 63 | +nums[i] =1; |
| 64 | + } |
| 65 | +for (inti =row;i <row +3;i++) { |
| 66 | +for (intj =col;j <col +3;j++) { |
| 67 | +if (board[i][j] !='.') |
| 68 | +nums[Character.getNumericValue(board[i][j]) -1]--; |
| 69 | + } |
| 70 | + } |
| 71 | +for (inti :nums) { |
| 72 | +if (i <0) |
| 73 | +returnfalse; |
| 74 | + } |
| 75 | +returntrue; |
| 76 | + } |
| 77 | + |
| 78 | +publicstaticvoidmain(String...strings) { |
| 79 | +ValidSudokutest =newValidSudoku(); |
| 80 | +// char[][] board = new char[][]{ |
| 81 | +// {'4', '3', '5', '2', '6', '9', '7', '8', '1'}, |
| 82 | +// {'6', '8', '2', '5', '7', '1', '4', '9', '3'}, |
| 83 | +// {'1', '9', '7', '8', '3', '4', '5', '6', '2'}, |
| 84 | +// {'8', '2', '6', '1', '9', '5', '3', '4', '7'}, |
| 85 | +// {'3', '7', '4', '6', '8', '2', '9', '1', '5'}, |
| 86 | +// {'9', '5', '1', '7', '4', '3', '6', '2', '8'}, |
| 87 | +// {'5', '1', '9', '3', '2', '6', '8', '7', '4'}, |
| 88 | +// {'2', '4', '8', '9', '5', '7', '1', '3', '6'}, |
| 89 | +// {'7', '6', '3', '4', '1', '8', '2', '5', '9'}, |
| 90 | +// }; |
| 91 | + |
| 92 | +// char[][] board = new char[][]{ |
| 93 | +// {'.', '8', '7', '6', '5', '4', '3', '2', '1'}, |
| 94 | +// {'2', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 95 | +// {'3', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 96 | +// {'4', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 97 | +// {'5', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 98 | +// {'6', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 99 | +// {'7', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 100 | +// {'8', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 101 | +// {'9', '.', '.', '.', '.', '.', '.', '.', '.'}, |
| 102 | +// }; |
| 103 | + |
| 104 | +char[][]board =newchar[][] { |
| 105 | + {'.','.','.','.','5','.','.','1','.' },// this upper right corner 3*3 |
| 106 | +// square is invalid, '1' appears |
| 107 | +// twice |
| 108 | + {'.','4','.','3','.','.','.','.','.' }, |
| 109 | + {'.','.','.','.','.','3','.','.','1' }, |
| 110 | + {'8','.','.','.','.','.','.','2','.' }, |
| 111 | + {'.','.','2','.','7','.','.','.','.' }, |
| 112 | + {'.','1','5','.','.','.','.','.','.' }, |
| 113 | + {'.','.','.','.','.','2','.','.','.' }, |
| 114 | + {'.','2','.','9','.','.','.','.','.' }, |
| 115 | + {'.','.','4','.','.','.','.','.','.' }, }; |
| 116 | + |
| 117 | +// ["....5..1.",".4.3.....",".....3..1","8......2.","..2.7....",".15......",".....2...",".2.9.....","..4......"] |
| 118 | + |
| 119 | +System.out.println(test.isValidSudoku(board)); |
| 120 | + } |
| 121 | +} |