Skip to content

Commit dff6ce2

Browse files
committed
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)
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