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

Commitbcea90a

Browse files
authored
Merge pull requestneetcode-gh#2482 from matt-botti/main
Update 0036-valid-sudoku.java
2 parents7de19ee +6314e86 commitbcea90a

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

‎java/0036-valid-sudoku.java

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,65 @@
11
classSolution {
22

33
publicbooleanisValidSudoku(char[][]board) {
4-
introws =board.length;
5-
intcols =board[0].length;
4+
//neetcode solution, slightly modified
65

6+
//a set of the characters that we have already come across (excluding '.' which denotes an empty space)
77
Set<Character>rowSet =null;
88
Set<Character>colSet =null;
99

10-
//check for rows
11-
for (inti =0;i <rows;i++) {
12-
rowSet =newHashSet<>();
13-
for (intj =0;j <cols;j++) {
14-
if (board[i][j] =='.') {
15-
continue;
16-
}
17-
if (rowSet.contains(board[i][j])) {
18-
returnfalse;
19-
}
20-
rowSet.add(board[i][j]);
21-
}
22-
}
2310

24-
//check for cols
25-
for (inti =0;i <cols;i++) {
11+
for (inti =0;i <9;i++) {
12+
//reinitialize the sets so we don't carry over found characters from the previous run
13+
rowSet =newHashSet<>();
2614
colSet =newHashSet<>();
27-
for (intj =0;j <rows;j++) {
28-
if (board[j][i] =='.') {
29-
continue;
15+
for (intj =0;j <9;j++) {
16+
charr =board[i][j];
17+
charc =board[j][i];
18+
if (r !='.'){
19+
if (rowSet.contains(r)){
20+
returnfalse;
21+
}else {
22+
rowSet.add(r);
23+
}
3024
}
31-
if (colSet.contains(board[j][i])) {
32-
returnfalse;
25+
if (c !='.'){
26+
if (colSet.contains(c)){
27+
returnfalse;
28+
}else {
29+
colSet.add(c);
30+
}
3331
}
34-
35-
colSet.add(board[j][i]);
3632
}
3733
}
3834

3935
//block
40-
for (inti =0;i <rows;i =i +3) {
41-
for (intj =0;j <cols;j =j +3) {
36+
//loop controls advance by 3 each time to jump through the boxes
37+
for (inti =0;i <9;i =i +3) {
38+
for (intj =0;j <9;j =j +3) {
39+
//checkBlock will return true if valid
4240
if (!checkBlock(i,j,board)) {
4341
returnfalse;
4442
}
4543
}
4644
}
47-
45+
//passed all tests, therefore valid board
4846
returntrue;
4947
}
5048

5149
publicbooleancheckBlock(intidxI,intidxJ,char[][]board) {
5250
Set<Character>blockSet =newHashSet<>();
51+
//if idxI = 3 and indJ = 0
52+
//rows = 6 and cols = 3
5353
introws =idxI +3;
5454
intcols =idxJ +3;
55+
//and because i initializes to idxI but only goes to rows, we loop 3 times (once for each row)
5556
for (inti =idxI;i <rows;i++) {
57+
//same for columns
5658
for (intj =idxJ;j <cols;j++) {
5759
if (board[i][j] =='.') {
5860
continue;
5961
}
60-
62+
6163
if (blockSet.contains(board[i][j])) {
6264
returnfalse;
6365
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp