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

Commitb0e1a66

Browse files
authored
Create 0399-evaluate-division.kt
1 parentc0aeeed commitb0e1a66

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

‎kotlin/0399-evaluate-division.kt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
classSolution {
2+
funcalcEquation(equations:List<List<String>>,values:DoubleArray,queries:List<List<String>>):DoubleArray {
3+
val adj=HashMap<String,ArrayList<Pair<String,Double>>>().apply {
4+
for (iin equations.indices) {
5+
val (a, b)= equations[i]
6+
val value= values[i]
7+
this[a]= getOrDefault(a,arrayListOf()).apply { add(b to value) }
8+
this[b]= getOrDefault(b,arrayListOf()).apply { add(a to (1.0/ value)) }
9+
}
10+
}
11+
12+
funbfs(
13+
start:String,
14+
end:String
15+
):Double {
16+
if (start!in adj|| end!in adj)
17+
return-1.0
18+
19+
val visit=HashSet<String>()
20+
with (LinkedList<Pair<String,Double>>()) {
21+
addLast(start to1.0)
22+
visit.add(start)
23+
24+
while (isNotEmpty()) {
25+
val (cur, totVal)= removeFirst()
26+
27+
if (cur== end)return totVal
28+
29+
adj[cur]?.forEach {
30+
val (next, nextVal)= it
31+
if (next!in visit) {
32+
addLast(next to (totVal* nextVal))
33+
visit.add(next)
34+
}
35+
}
36+
}
37+
}
38+
39+
return-1.0
40+
}
41+
42+
val res=DoubleArray(queries.size)
43+
for (iin queries.indices) {
44+
val (a, b)= queries[i]
45+
res[i]= bfs(a, b)
46+
}
47+
48+
return res
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp