Skip to content

Commit 97a2f53

Browse files
refactor 36
1 parent fe291d2 commit 97a2f53

File tree

3 files changed

+152
-112
lines changed

3 files changed

+152
-112
lines changed

src/main/java/com/fishercoder/solutions/_30.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public List<Integer> findSubstring(String s, String[] words) {
3434
for (int i = 0; i < s.length(); i++) {
3535
startIndex = i;
3636
Map<String, Boolean> clone = new HashMap<>(map);
37-
for (int j = i +1; j < s.length(); j++) {
37+
for (int j = i + 1; j < s.length(); j++) {
3838
String word = s.substring(i, j);
3939
if (clone.containsKey(word) && clone.get(word)) {
4040
clone.put(word, false);
41-
i = j+1;
41+
i = j + 1;
4242
} else {
4343
break;
4444
}
Lines changed: 66 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,95 @@
11
package com.fishercoder.solutions;
22

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.
910
1011
Note:
1112
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.*/
1213
public class _36 {
1314

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 {
1716
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;
2220
}
21+
}
2322

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;
2826
}
27+
}
2928

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+
}
3634
}
37-
return true;
35+
}
36+
return true;
3837
}
3938

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]--;
4447
}
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;
4952
}
50-
for (int i : nums) {
51-
if (i < 0) {
52-
return false;
53-
}
54-
}
55-
return true;
53+
}
54+
return true;
5655
}
5756

5857
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]--;
6765
}
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;
7270
}
73-
return true;
71+
}
72+
return true;
7473
}
7574

7675
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+
}
8785
}
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;
9290
}
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;
13893
}
94+
}
13995
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._36;
4+
import com.fishercoder.solutions._735;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static junit.framework.TestCase.assertEquals;
9+
import static org.junit.Assert.assertArrayEquals;
10+
11+
public class _36Test {
12+
private static _36.Solution1 solution1;
13+
private static char[][] board;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _36.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
board = new char[][] {
23+
{'4', '3', '5', '2', '6', '9', '7', '8', '1'},
24+
{'6', '8', '2', '5', '7', '1', '4', '9', '3'},
25+
{'1', '9', '7', '8', '3', '4', '5', '6', '2'},
26+
{'8', '2', '6', '1', '9', '5', '3', '4', '7'},
27+
{'3', '7', '4', '6', '8', '2', '9', '1', '5'},
28+
{'9', '5', '1', '7', '4', '3', '6', '2', '8'},
29+
{'5', '1', '9', '3', '2', '6', '8', '7', '4'},
30+
{'2', '4', '8', '9', '5', '7', '1', '3', '6'},
31+
{'7', '6', '3', '4', '1', '8', '2', '5', '9'},
32+
};
33+
assertEquals(true, solution1.isValidSudoku(board));
34+
}
35+
36+
@Test
37+
public void test2() {
38+
board = new char[][] {
39+
{'.', '8', '7', '6', '5', '4', '3', '2', '1'},
40+
{'2', '.', '.', '.', '.', '.', '.', '.', '.'},
41+
{'3', '.', '.', '.', '.', '.', '.', '.', '.'},
42+
{'4', '.', '.', '.', '.', '.', '.', '.', '.'},
43+
{'5', '.', '.', '.', '.', '.', '.', '.', '.'},
44+
{'6', '.', '.', '.', '.', '.', '.', '.', '.'},
45+
{'7', '.', '.', '.', '.', '.', '.', '.', '.'},
46+
{'8', '.', '.', '.', '.', '.', '.', '.', '.'},
47+
{'9', '.', '.', '.', '.', '.', '.', '.', '.'},
48+
};
49+
assertEquals(true, solution1.isValidSudoku(board));
50+
}
51+
52+
@Test
53+
public void test3() {
54+
board = new char[][] {
55+
{'.', '.', '.', '.', '5', '.', '.', '1', '.'},
56+
// this upper right corner 3*3 square is invalid, '1' appears twice
57+
{'.', '4', '.', '3', '.', '.', '.', '.', '.'},
58+
{'.', '.', '.', '.', '.', '3', '.', '.', '1'},
59+
{'8', '.', '.', '.', '.', '.', '.', '2', '.'},
60+
{'.', '.', '2', '.', '7', '.', '.', '.', '.'},
61+
{'.', '1', '5', '.', '.', '.', '.', '.', '.'},
62+
{'.', '.', '.', '.', '.', '2', '.', '.', '.'},
63+
{'.', '2', '.', '9', '.', '.', '.', '.', '.'},
64+
{'.', '.', '4', '.', '.', '.', '.', '.', '.'},
65+
};
66+
assertEquals(false, solution1.isValidSudoku(board));
67+
}
68+
69+
@Test
70+
public void test4() {
71+
board = new char[][] {
72+
{'.', '.', '4', '.', '.', '.', '6', '3', '.'},
73+
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
74+
{'5', '.', '.', '.', '.', '.', '.', '9', '.'},
75+
{'.', '.', '.', '5', '6', '.', '.', '.', '.'},
76+
{'4', '.', '3', '.', '.', '.', '.', '.', '1'},
77+
{'.', '.', '.', '7', '.', '.', '.', '.', '.'},
78+
{'.', '.', '.', '5', '.', '.', '.', '.', '.'},
79+
{'.', '.', '.', '.', '.', '.', '.', '.', '.'},
80+
{'.', '.', '.', '.', '.', '.', '.', '.', '.'}
81+
};
82+
assertEquals(false, solution1.isValidSudoku(board));
83+
}
84+
}

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