Skip to content

Commit 24341d3

Browse files
refactor 36
1 parent 457b738 commit 24341d3

File tree

1 file changed

+70
-80
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+70
-80
lines changed
Lines changed: 70 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,85 @@
11
package com.fishercoder.solutions;
22

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.
10-
11-
Note:
12-
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.*/
133
public class _36 {
144

15-
public static class Solution1 {
16-
public boolean isValidSudoku(char[][] board) {
17-
for (int i = 0; i < 9; i++) {
18-
if (!isValidRowOrColumn(board, i)) {
19-
return false;
20-
}
21-
}
5+
public static class Solution1 {
6+
public boolean isValidSudoku(char[][] board) {
7+
for (int i = 0; i < 9; i++) {
8+
if (!isValidRowOrColumn(board, i)) {
9+
return false;
10+
}
11+
}
2212

23-
for (int j = 0; j < 9; j++) {
24-
if (!isValidCol(board, j)) {
25-
return false;
26-
}
27-
}
13+
for (int j = 0; j < 9; j++) {
14+
if (!isValidCol(board, j)) {
15+
return false;
16+
}
17+
}
2818

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-
}
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+
}
26+
return true;
3427
}
35-
}
36-
return true;
37-
}
3828

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]--;
47-
}
48-
}
49-
for (int i : nums) {
50-
if (i < 0) {
51-
return false;
29+
boolean isValidRowOrColumn(char[][] board, int index) {
30+
int[] nums = new int[9];
31+
for (int i = 0; i < 9; i++) {
32+
nums[i] = 1;
33+
}
34+
for (int j = 0; j < 9; j++) {
35+
if (board[index][j] != '.') {
36+
nums[Character.getNumericValue(board[index][j]) - 1]--;
37+
}
38+
}
39+
for (int i : nums) {
40+
if (i < 0) {
41+
return false;
42+
}
43+
}
44+
return true;
5245
}
53-
}
54-
return true;
55-
}
5646

57-
boolean isValidCol(char[][] board, int col) {
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]--;
65-
}
66-
}
67-
for (int i : nums) {
68-
if (i < 0) {
69-
return false;
47+
boolean isValidCol(char[][] board, int col) {
48+
int[] nums = new int[9];
49+
for (int i = 0; i < 9; i++) {
50+
nums[i] = 1;
51+
}
52+
for (int i = 0; i < 9; i++) {
53+
if (board[i][col] != '.') {
54+
nums[Character.getNumericValue(board[i][col]) - 1]--;
55+
}
56+
}
57+
for (int i : nums) {
58+
if (i < 0) {
59+
return false;
60+
}
61+
}
62+
return true;
7063
}
71-
}
72-
return true;
73-
}
7464

75-
boolean isValidSquare(char[][] board, int row, int col) {
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-
}
85-
}
86-
}
87-
for (int i : nums) {
88-
if (i < 0) {
89-
return false;
65+
boolean isValidSquare(char[][] board, int row, int col) {
66+
int[] nums = new int[9];
67+
for (int i = 0; i < 9; i++) {
68+
nums[i] = 1;
69+
}
70+
for (int i = row; i < row + 3; i++) {
71+
for (int j = col; j < col + 3; j++) {
72+
if (board[i][j] != '.') {
73+
nums[Character.getNumericValue(board[i][j]) - 1]--;
74+
}
75+
}
76+
}
77+
for (int i : nums) {
78+
if (i < 0) {
79+
return false;
80+
}
81+
}
82+
return true;
9083
}
91-
}
92-
return true;
9384
}
94-
}
9585
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy