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

Commit34d4fc4

Browse files
authored
Create 0071-simplify-path.kt
1 parent050c9c5 commit34d4fc4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

‎kotlin/0071-simplify-path.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
classSolution {
2+
funsimplifyPath(path:String):String {
3+
val stack=LinkedList<String>()
4+
val invalids= hashSetOf("",".","..")
5+
6+
path.split("/").forEach{
7+
if(it==".."&& stack.isNotEmpty()) {
8+
stack.removeLast()
9+
}elseif (it!in invalids) {
10+
stack.addLast(it)
11+
}
12+
}
13+
14+
returnif(stack.isEmpty())"/"elseStringBuilder().apply{
15+
while (stack.isNotEmpty()) {
16+
this.append("/")
17+
this.append(stack.removeFirst())
18+
}
19+
}.toString()
20+
}
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp