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

Commit dff6ce2

Browse files
Time: 3 ms (91.93%), Space: 48.9 MB (42.67%) - LeetHub
1 parent b376782 commit dff6ce2

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution {
2+
private fun BooleanArray.clear() {
3+
for (i in indices) {
4+
this[i] = false
5+
}
6+
}
7+
8+
fun isValidSudoku(board: Array<CharArray>): Boolean {
9+
val isFilled = BooleanArray(10)
10+
11+
for (row in board) {
12+
isFilled.clear()
13+
14+
for (char in row) {
15+
if (char == '.') {
16+
continue
17+
}
18+
19+
val number = char.digitToInt()
20+
if (isFilled[number]) {
21+
return false
22+
}
23+
isFilled[number] = true
24+
}
25+
}
26+
27+
for (x in 0 until 9) {
28+
isFilled.clear()
29+
30+
for (y in 0 until 9) {
31+
val char = board[y][x]
32+
33+
if (char == '.') {
34+
continue
35+
}
36+
37+
val number = char.digitToInt()
38+
if (isFilled[number]) {
39+
return false
40+
}
41+
isFilled[number] = true
42+
}
43+
}
44+
45+
for (gridY in 0 until 3) {
46+
for (gridX in 0 until 3) {
47+
isFilled.clear()
48+
49+
val startY = gridY * 3
50+
val startX = gridX * 3
51+
for (y in startY until startY + 3) {
52+
for (x in startX until startX + 3) {
53+
val char = board[y][x]
54+
55+
if (char == '.') {
56+
continue
57+
}
58+
59+
val number = char.digitToInt()
60+
if (isFilled[number]) {
61+
return false
62+
}
63+
isFilled[number] = true
64+
}
65+
}
66+
}
67+
}
68+
69+
return true
70+
}
71+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /