Skip to content

Commit d2d100b

Browse files
Valid soduku (#195)
* ValidSudoku * valid sudoku
1 parent 85198e1 commit d2d100b

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.examplehub.leetcode.middle;
2+
3+
/**
4+
* https://leetcode.com/problems/valid-sudoku/
5+
*/
6+
public class ValidSudoku {
7+
public static boolean solution1(char[][] board) {
8+
9+
//travel all rows to check
10+
for (int i = 0; i < 9; i++) {
11+
int[] digitTable = new int[10];
12+
for (int j = 0; j < 9; j++) {
13+
if (board[i][j] != '.') {
14+
digitTable[board[i][j] - '0']++;
15+
}
16+
}
17+
if (containsRepeat(digitTable)) {
18+
return false;
19+
}
20+
}
21+
22+
//travel all columns to check
23+
for (int i = 0; i < 9; i++) {
24+
int[] digitTable = new int[10];
25+
for (int j = 0; j < 9; j++) {
26+
if (board[j][i] != '.') {
27+
digitTable[board[j][i] - '0']++;
28+
}
29+
}
30+
if (containsRepeat(digitTable)) {
31+
return false;
32+
}
33+
}
34+
35+
//travel all sub-box
36+
//TODO
37+
return true;
38+
}
39+
40+
public static boolean containsRepeat(int[] table) {
41+
for (int num : table) {
42+
if (num > 1) {
43+
return true;
44+
}
45+
}
46+
return false;
47+
}
48+
49+
public static boolean solution2(char[][] board) {
50+
int[][] rows = new int[9][9];
51+
int[][] cols = new int[9][9];
52+
int[][][] subBoxes = new int[3][3][9];
53+
for (int i = 0; i < 9; ++i) {
54+
for (int j = 0; j < 9; ++j) {
55+
if (board[i][j] != '.') {
56+
int index = board[i][j] - '1';
57+
rows[i][index]++;
58+
cols[j][index]++;
59+
subBoxes[i / 3][j / 3][index]++;
60+
if (rows[i][index] > 1 || cols[j][index] > 1 || subBoxes[i / 3][j / 3][index] > 1) {
61+
return false;
62+
}
63+
}
64+
}
65+
}
66+
return true;
67+
}
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
2+
3+
package com.examplehub.leetcode.middle;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.*;
8+
9+
class ValidSudokuTest {
10+
11+
@Test
12+
void testSolution1() {
13+
char[][] board = {
14+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
15+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
16+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
17+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
18+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
19+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
20+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
21+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
22+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
23+
};
24+
assertTrue(ValidSudoku.solution1(board));
25+
26+
board = new char[][]{
27+
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
28+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
29+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
30+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
31+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
32+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
33+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
34+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
35+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
36+
};
37+
assertFalse(ValidSudoku.solution1(board));
38+
}
39+
40+
@Test
41+
void testSolution2() {
42+
char[][] board = {
43+
{'5', '3', '.', '.', '7', '.', '.', '.', '.'},
44+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
45+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
46+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
47+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
48+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
49+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
50+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
51+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
52+
};
53+
assertTrue(ValidSudoku.solution2(board));
54+
55+
board = new char[][]{
56+
{'8', '3', '.', '.', '7', '.', '.', '.', '.'},
57+
{'6', '.', '.', '1', '9', '5', '.', '.', '.'},
58+
{'.', '9', '8', '.', '.', '.', '.', '6', '.'},
59+
{'8', '.', '.', '.', '6', '.', '.', '.', '3'},
60+
{'4', '.', '.', '8', '.', '3', '.', '.', '1'},
61+
{'7', '.', '.', '.', '2', '.', '.', '.', '6'},
62+
{'.', '6', '.', '.', '.', '.', '2', '8', '.'},
63+
{'.', '.', '.', '4', '1', '9', '.', '.', '5'},
64+
{'.', '.', '.', '.', '8', '.', '.', '7', '9'},
65+
};
66+
assertFalse(ValidSudoku.solution2(board));
67+
}
68+
}

0 commit comments

Comments
 (0)