We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f917a49 commit b0d6878Copy full SHA for b0d6878
227. Basic Calculator II.kt
@@ -0,0 +1,30 @@
1
+class Solution {
2
+ fun calculate(s: String): Int {
3
+ val stack = mutableListOf<Int>()
4
+ var num = 0
5
+ var sign = '+'
6
+
7
+ for (char in s) {
8
+ if (char.isDigit()) {
9
+ num = num * 10 + (char - '0')
10
+ } else if (char != ' ') {
11
+ when (sign) {
12
+ '+' -> stack.add(num)
13
+ '-' -> stack.add(-num)
14
+ '*' -> stack.add(stack.removeAt(stack.size - 1) * num)
15
+ '/' -> stack.add(stack.removeAt(stack.size - 1) / num)
16
+ }
17
+ sign = char
18
+ num = 0
19
20
21
22
23
24
25
26
27
28
+ return stack.sum()
29
30
+}
0 commit comments