|
| 1 | +package easy; |
| 2 | + |
| 3 | +public class ValidSudoku { |
| 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 | + public boolean isValidSudoku(char[][] board) { |
| 9 | + for (int i = 0; i < 9; i++) { |
| 10 | + if (!isValidRow(board, i)) |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | + for (int j = 0; j < 9; j++) { |
| 15 | + if (!isValidCol(board, j)) |
| 16 | + return false; |
| 17 | + } |
| 18 | + |
| 19 | + for (int i = 0; i < 7; i = i + 3) { |
| 20 | + for (int j = 0; j < 7; j = j + 3) { |
| 21 | + if (!isValidSquare(board, i, j)) |
| 22 | + return false; |
| 23 | + } |
| 24 | + } |
| 25 | + return true; |
| 26 | + } |
| 27 | + |
| 28 | + boolean isValidRow(char[][] board, int row) { |
| 29 | + int[] nums = new int[9]; |
| 30 | + for (int i = 0; i < 9; i++) { |
| 31 | + nums[i] = 1; |
| 32 | + } |
| 33 | + for (int j = 0; j < 9; j++) { |
| 34 | + if (board[row][j] != '.') |
| 35 | + nums[Character.getNumericValue(board[row][j]) - 1]--; |
| 36 | + } |
| 37 | + for (int i : nums) { |
| 38 | + if (i < 0) |
| 39 | + return false; |
| 40 | + } |
| 41 | + return true; |
| 42 | + } |
| 43 | + |
| 44 | + boolean isValidCol(char[][] board, int col) { |
| 45 | + int[] nums = new int[9]; |
| 46 | + for (int i = 0; i < 9; i++) { |
| 47 | + nums[i] = 1; |
| 48 | + } |
| 49 | + for (int i = 0; i < 9; i++) { |
| 50 | + if (board[i][col] != '.') |
| 51 | + nums[Character.getNumericValue(board[i][col]) - 1]--; |
| 52 | + } |
| 53 | + for (int i : nums) { |
| 54 | + if (i < 0) |
| 55 | + return false; |
| 56 | + } |
| 57 | + return true; |
| 58 | + } |
| 59 | + |
| 60 | + boolean isValidSquare(char[][] board, int row, int col) { |
| 61 | + int[] nums = new int[9]; |
| 62 | + for (int i = 0; i < 9; i++) { |
| 63 | + nums[i] = 1; |
| 64 | + } |
| 65 | + for (int i = row; i < row + 3; i++) { |
| 66 | + for (int j = col; j < col + 3; j++) { |
| 67 | + if (board[i][j] != '.') |
| 68 | + nums[Character.getNumericValue(board[i][j]) - 1]--; |
| 69 | + } |
| 70 | + } |
| 71 | + for (int i : nums) { |
| 72 | + if (i < 0) |
| 73 | + return false; |
| 74 | + } |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + public static void main(String... strings) { |
| 79 | + ValidSudoku test = new ValidSudoku(); |
| 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 = new char[][] { |
| 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 | +} |
0 commit comments