Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf5bc05b

Browse files
committed
valid
1 parent78d523d commitf5bc05b

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

‎hash/IsValidSudoku.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
packageAlgorithms.hash;
2+
3+
importjava.util.HashSet;
4+
5+
publicclassIsValidSudoku {
6+
publicstaticvoidmain(String[]strs) {
7+
8+
}
9+
10+
publicstaticbooleanisValidSudoku(char[][]board) {
11+
if (board ==null ||board.length !=9 ||
12+
board[0].length !=9) {
13+
returnfalse;
14+
}
15+
16+
HashSet<Character>set =newHashSet<Character>();
17+
18+
// check the rows.
19+
for (inti =0;i <9;i++) {
20+
// clear the set at every row.
21+
set.clear();
22+
for (intj =0;j <9;j++) {
23+
if (!isValidChar(board[i][j],set)) {
24+
returnfalse;
25+
}
26+
}
27+
}
28+
29+
// check the columns.
30+
for (inti =0;i <9;i++) {
31+
// clear the set at every column.
32+
set.clear();
33+
for (intj =0;j <9;j++) {
34+
if (!isValidChar(board[j][i],set)) {
35+
returnfalse;
36+
}
37+
}
38+
}
39+
40+
// check the blocks.
41+
for (inti =0;i <3;i++) {
42+
for (intj =0;j <3;j++) {
43+
// clear the set at every block.
44+
set.clear();
45+
for (intk =0;k <9;k++) {
46+
if (!isValidChar(board[i +k /3][j +k %3],set)) {
47+
returnfalse;
48+
}
49+
}
50+
}
51+
}
52+
53+
returntrue;
54+
}
55+
56+
publicstaticbooleanisValidChar(charc,HashSet<Character>set) {
57+
if (c =='.') {
58+
returntrue;
59+
}
60+
61+
if (c <'0' ||c >'9') {
62+
returnfalse;
63+
}
64+
65+
// Check if the character exit in the hashset.
66+
if (set.contains(c)) {
67+
returnfalse;
68+
}
69+
70+
set.add(c);
71+
72+
returntrue;
73+
}
74+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp