-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
54 lines (51 loc) · 1.97 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.company;
public class Main {
public static void main(String[] args) {
char[][] board = {
{'.', '.', '.', '.', '5', '.', '.', '1', '.' },
{'.', '4', '.', '3', '.', '.', '.', '.', '.' },
{'.', '.', '.', '.', '.', '3', '.', '.', '1' },
{'8', '.', '.', '.', '.', '.', '.', '2', '.' },
{'.', '.', '2', '.', '7', '.', '.', '.', '.' },
{'.', '1', '5', '.', '.', '.', '.', '.', '.' },
{'.', '.', '.', '.', '.', '2', '.', '.', '.' },
{'.', '2', '.', '9', '.', '.', '.', '.', '.' },
{'.', '.', '4', '.', '.', '.', '.', '.', '.' },
};
System.out.println(isValidSudoku(board));
}
public static boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
var record = new int[10];
for (int j = 0; j < 9; j++) {
if (board[i][j] == '.') continue;
record[board[i][j] - '0']++;
if (record[board[i][j] - '0'] > 1)
return false;
}
}
for (int j = 0; j < 9; j++) {
var record = new int[10];
for (int i = 0; i < 9; i++) {
if (board[i][j] == '.') continue;
record[board[i][j] - '0']++;
if (record[board[i][j] - '0'] > 1)
return false;
}
}
for (int row = 0; row < 9; row += 3) {
for (int col = 0; col < 9; col += 3) {
var record = new int[10];
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
if (board[i][j] == '.') continue;
record[board[i][j] - '0']++;
if (record[board[i][j] - '0'] > 1)
return false;
}
}
}
}
return true;
}
}