|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | | -/**Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. |
4 | | -
|
5 | | - The Sudoku board could be partially filled, where empty cells are filled with the character '.'. |
6 | | -
|
7 | | -
|
8 | | - A partially filled sudoku which is valid. |
| 3 | +/** |
| 4 | + * 36. Valid Sudoku |
| 5 | + * |
| 6 | + * Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. |
| 7 | + * The Sudoku board could be partially filled, where empty cells are filled with the character '.'. |
| 8 | + * |
| 9 | + * A partially filled sudoku which is valid. |
9 | 10 |
|
10 | 11 | Note: |
11 | 12 | A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.*/ |
12 | 13 | public class _36 { |
13 | 14 |
|
14 | | - // this is my original solution, pretty straightforward, but lengthy, there's a very concise |
15 | | - // version: https://discuss.leetcode.com/topic/9748/shared-my-concise-java-code, it uses |
16 | | - //three HashSets in each loop, pretty cool! |
| 15 | + public static class Solution1 { |
17 | 16 | public boolean isValidSudoku(char[][] board) { |
18 | | - for (int i = 0; i < 9; i++) { |
19 | | - if (!isValidRow(board, i)) { |
20 | | - return false; |
21 | | - } |
| 17 | + for (int i = 0; i < 9; i++) { |
| 18 | + if (!isValidRowOrColumn(board, i)) { |
| 19 | + return false; |
22 | 20 | } |
| 21 | + } |
23 | 22 |
|
24 | | - for (int j = 0; j < 9; j++) { |
25 | | - if (!isValidCol(board, j)) { |
26 | | - return false; |
27 | | - } |
| 23 | + for (int j = 0; j < 9; j++) { |
| 24 | + if (!isValidCol(board, j)) { |
| 25 | + return false; |
28 | 26 | } |
| 27 | + } |
29 | 28 |
|
30 | | - for (int i = 0; i < 7; i = i + 3) { |
31 | | - for (int j = 0; j < 7; j = j + 3) { |
32 | | - if (!isValidSquare(board, i, j)) { |
33 | | - return false; |
34 | | - } |
35 | | - } |
| 29 | + for (int i = 0; i < 7; i = i + 3) { |
| 30 | + for (int j = 0; j < 7; j = j + 3) { |
| 31 | + if (!isValidSquare(board, i, j)) { |
| 32 | + return false; |
| 33 | + } |
36 | 34 | } |
37 | | - return true; |
| 35 | + } |
| 36 | + return true; |
38 | 37 | } |
39 | 38 |
|
40 | | - boolean isValidRow(char[][] board, int row) { |
41 | | - int[] nums = new int[9]; |
42 | | - for (int i = 0; i < 9; i++) { |
43 | | - nums[i] = 1; |
| 39 | + boolean isValidRowOrColumn(char[][] board, int index) { |
| 40 | + int[] nums = new int[9]; |
| 41 | + for (int i = 0; i < 9; i++) { |
| 42 | + nums[i] = 1; |
| 43 | + } |
| 44 | + for (int j = 0; j < 9; j++) { |
| 45 | + if (board[index][j] != '.') { |
| 46 | + nums[Character.getNumericValue(board[index][j]) - 1]--; |
44 | 47 | } |
45 | | - for (int j = 0; j < 9; j++) { |
46 | | - if (board[row][j] != '.') { |
47 | | - nums[Character.getNumericValue(board[row][j]) - 1]--; |
48 | | - } |
| 48 | + } |
| 49 | + for (int i : nums) { |
| 50 | + if (i < 0) { |
| 51 | + return false; |
49 | 52 | } |
50 | | - for (int i : nums) { |
51 | | - if (i < 0) { |
52 | | - return false; |
53 | | - } |
54 | | - } |
55 | | - return true; |
| 53 | + } |
| 54 | + return true; |
56 | 55 | } |
57 | 56 |
|
58 | 57 | boolean isValidCol(char[][] board, int col) { |
59 | | - int[] nums = new int[9]; |
60 | | - for (int i = 0; i < 9; i++) { |
61 | | - nums[i] = 1; |
62 | | - } |
63 | | - for (int i = 0; i < 9; i++) { |
64 | | - if (board[i][col] != '.') { |
65 | | - nums[Character.getNumericValue(board[i][col]) - 1]--; |
66 | | - } |
| 58 | + int[] nums = new int[9]; |
| 59 | + for (int i = 0; i < 9; i++) { |
| 60 | + nums[i] = 1; |
| 61 | + } |
| 62 | + for (int i = 0; i < 9; i++) { |
| 63 | + if (board[i][col] != '.') { |
| 64 | + nums[Character.getNumericValue(board[i][col]) - 1]--; |
67 | 65 | } |
68 | | - for (int i : nums) { |
69 | | - if (i < 0) { |
70 | | - return false; |
71 | | - } |
| 66 | + } |
| 67 | + for (int i : nums) { |
| 68 | + if (i < 0) { |
| 69 | + return false; |
72 | 70 | } |
73 | | - return true; |
| 71 | + } |
| 72 | + return true; |
74 | 73 | } |
75 | 74 |
|
76 | 75 | boolean isValidSquare(char[][] board, int row, int col) { |
77 | | - int[] nums = new int[9]; |
78 | | - for (int i = 0; i < 9; i++) { |
79 | | - nums[i] = 1; |
80 | | - } |
81 | | - for (int i = row; i < row + 3; i++) { |
82 | | - for (int j = col; j < col + 3; j++) { |
83 | | - if (board[i][j] != '.') { |
84 | | - nums[Character.getNumericValue(board[i][j]) - 1]--; |
85 | | - } |
86 | | - } |
| 76 | + int[] nums = new int[9]; |
| 77 | + for (int i = 0; i < 9; i++) { |
| 78 | + nums[i] = 1; |
| 79 | + } |
| 80 | + for (int i = row; i < row + 3; i++) { |
| 81 | + for (int j = col; j < col + 3; j++) { |
| 82 | + if (board[i][j] != '.') { |
| 83 | + nums[Character.getNumericValue(board[i][j]) - 1]--; |
| 84 | + } |
87 | 85 | } |
88 | | - for (int i : nums) { |
89 | | - if (i < 0) { |
90 | | - return false; |
91 | | - } |
| 86 | + } |
| 87 | + for (int i : nums) { |
| 88 | + if (i < 0) { |
| 89 | + return false; |
92 | 90 | } |
93 | | - return true; |
94 | | - } |
95 | | - |
96 | | - public static void main(String... strings) { |
97 | | - _36 test = new _36(); |
98 | | - // char[][] board = new char[][]{ |
99 | | - // {'4', '3', '5', '2', '6', '9', '7', '8', '1'}, |
100 | | - // {'6', '8', '2', '5', '7', '1', '4', '9', '3'}, |
101 | | - // {'1', '9', '7', '8', '3', '4', '5', '6', '2'}, |
102 | | - // {'8', '2', '6', '1', '9', '5', '3', '4', '7'}, |
103 | | - // {'3', '7', '4', '6', '8', '2', '9', '1', '5'}, |
104 | | - // {'9', '5', '1', '7', '4', '3', '6', '2', '8'}, |
105 | | - // {'5', '1', '9', '3', '2', '6', '8', '7', '4'}, |
106 | | - // {'2', '4', '8', '9', '5', '7', '1', '3', '6'}, |
107 | | - // {'7', '6', '3', '4', '1', '8', '2', '5', '9'}, |
108 | | - // }; |
109 | | - |
110 | | - // char[][] board = new char[][]{ |
111 | | - // {'.', '8', '7', '6', '5', '4', '3', '2', '1'}, |
112 | | - // {'2', '.', '.', '.', '.', '.', '.', '.', '.'}, |
113 | | - // {'3', '.', '.', '.', '.', '.', '.', '.', '.'}, |
114 | | - // {'4', '.', '.', '.', '.', '.', '.', '.', '.'}, |
115 | | - // {'5', '.', '.', '.', '.', '.', '.', '.', '.'}, |
116 | | - // {'6', '.', '.', '.', '.', '.', '.', '.', '.'}, |
117 | | - // {'7', '.', '.', '.', '.', '.', '.', '.', '.'}, |
118 | | - // {'8', '.', '.', '.', '.', '.', '.', '.', '.'}, |
119 | | - // {'9', '.', '.', '.', '.', '.', '.', '.', '.'}, |
120 | | - // }; |
121 | | - |
122 | | - char[][] board = new char[][] { |
123 | | - { '.', '.', '.', '.', '5', '.', '.', '1', '.' },// this upper right corner 3*3 |
124 | | - // square is invalid, '1' appears |
125 | | - // twice |
126 | | - { '.', '4', '.', '3', '.', '.', '.', '.', '.' }, |
127 | | - { '.', '.', '.', '.', '.', '3', '.', '.', '1' }, |
128 | | - { '8', '.', '.', '.', '.', '.', '.', '2', '.' }, |
129 | | - { '.', '.', '2', '.', '7', '.', '.', '.', '.' }, |
130 | | - { '.', '1', '5', '.', '.', '.', '.', '.', '.' }, |
131 | | - { '.', '.', '.', '.', '.', '2', '.', '.', '.' }, |
132 | | - { '.', '2', '.', '9', '.', '.', '.', '.', '.' }, |
133 | | - { '.', '.', '4', '.', '.', '.', '.', '.', '.' }, }; |
134 | | - |
135 | | - // ["....5..1.",".4.3.....",".....3..1","8......2.","..2.7....",".15......",".....2...",".2.9.....","..4......"] |
136 | | - |
137 | | - System.out.println(test.isValidSudoku(board)); |
| 91 | + } |
| 92 | + return true; |
138 | 93 | } |
| 94 | + } |
139 | 95 | } |
0 commit comments