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

Commitb9c25c1

Browse files
committed
Time: 25 ms (29.08%), Space: 39 MB (43.14%) - LeetHub
1 parent7224aee commitb9c25c1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
importjava.util.Stack
2+
3+
classSolution {
4+
privatevalOPERATORS= arrayOf(
5+
"+","-","*","/"
6+
)
7+
8+
funevalRPN(tokens:Array<String>):Int {
9+
val numbers=Stack<Int>()
10+
11+
for (tokenin tokens) {
12+
if (token!inOPERATORS) {
13+
numbers.push(token.toInt())
14+
}else {
15+
val op2= numbers.pop()
16+
val op1= numbers.pop()
17+
18+
val result=when (token) {
19+
"+"-> op1+ op2
20+
"-"-> op1- op2
21+
"*"-> op1* op2
22+
else-> op1/ op2
23+
}
24+
25+
numbers.push(result)
26+
}
27+
}
28+
29+
return numbers.pop()
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp