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

Commit5f86a4f

Browse files
authored
Merge pull requestneetcode-gh#3227 from themlkang42/0286-walls-and-gates-kotlin
Add: 0286-walls-and-gates.kt
2 parents4b208ad +63a186d commit5f86a4f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎kotlin/0286-walls-and-gates.kt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classSolution {
2+
funwallsAndGates(rooms:Array<IntArray>):Unit {
3+
val queue:Queue<Pair<Int,Int>>=LinkedList()
4+
for (iin0 until rooms.size) {
5+
for (jin0 until rooms[0].size) {
6+
if (rooms[i][j]==0) {
7+
queue.offer(Pair(i, j))
8+
}
9+
}
10+
}
11+
12+
val directions= arrayOf(arrayOf(-1,0), arrayOf(1,0), arrayOf(0,-1), arrayOf(0,1))
13+
var distance=1
14+
while (queue.isNotEmpty()) {
15+
val size= queue.size
16+
repeat(size) {
17+
val cell= queue.poll()
18+
for (directionin directions) {
19+
val newRow= cell.first+direction[0]
20+
val newCol= cell.second+direction[1]
21+
if (newRow>=0&& newRow< rooms.size&& newCol>=0&& newCol< rooms[0].size&& rooms[newRow][newCol]==Integer.MAX_VALUE) {
22+
rooms[newRow][newCol]= distance
23+
queue.offer(Pair(newRow, newCol))
24+
}
25+
}
26+
}
27+
distance++
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp