forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_36.java
85 lines (78 loc) · 2.41 KB
/
_36.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.fishercoder.solutions;
public class _36 {
public static class Solution1 {
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
if (!isValidRowOrColumn(board, i)) {
return false;
}
}
for (int j = 0; j < 9; j++) {
if (!isValidCol(board, j)) {
return false;
}
}
for (int i = 0; i < 7; i = i + 3) {
for (int j = 0; j < 7; j = j + 3) {
if (!isValidSquare(board, i, j)) {
return false;
}
}
}
return true;
}
boolean isValidRowOrColumn(char[][] board, int index) {
int[] nums = new int[9];
for (int i = 0; i < 9; i++) {
nums[i] = 1;
}
for (int j = 0; j < 9; j++) {
if (board[index][j] != '.') {
nums[Character.getNumericValue(board[index][j]) - 1]--;
}
}
for (int i : nums) {
if (i < 0) {
return false;
}
}
return true;
}
boolean isValidCol(char[][] board, int col) {
int[] nums = new int[9];
for (int i = 0; i < 9; i++) {
nums[i] = 1;
}
for (int i = 0; i < 9; i++) {
if (board[i][col] != '.') {
nums[Character.getNumericValue(board[i][col]) - 1]--;
}
}
for (int i : nums) {
if (i < 0) {
return false;
}
}
return true;
}
boolean isValidSquare(char[][] board, int row, int col) {
int[] nums = new int[9];
for (int i = 0; i < 9; i++) {
nums[i] = 1;
}
for (int i = row; i < row + 3; i++) {
for (int j = col; j < col + 3; j++) {
if (board[i][j] != '.') {
nums[Character.getNumericValue(board[i][j]) - 1]--;
}
}
}
for (int i : nums) {
if (i < 0) {
return false;
}
}
return true;
}
}
}