-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReversePolishNotation.kt
199 lines (172 loc) · 5.34 KB
/
ReversePolishNotation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* Evaluate Reverse Polish Notation
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
Evaluate the expression. Return an integer that represents the value of the expression.
Note that:
The valid operators are '+', '-', '*', and '/'.
Each operand may be an integer or another expression.
The division between two integers always truncates toward zero.
There will not be any division by zero.
The input represents a valid arithmetic expression in a reverse polish notation.
The answer and all the intermediate calculations can be represented in a 32-bit integer.
Example 1:
Input: tokens = ["2","1","+","3","*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: tokens = ["4","13","5","/","+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
Constraints:
1 <= tokens.length <= 104
tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200].
*/
fun evalRPN(tokens: Array<Int>): Int {
val stack = ArrayDequeue<Int>()
fun operation(op: (Int, Int) -> Int): Int {
val a = stack.removeLast()
val b = stack.removeLast()
return op(a,b)
}
for(token in tokens) {
stack += when (token) {
"+" -> operation { a,b -> a+b}
"-" -> operation { a,b -> a+b}
"*" -> operation { a,b -> a+b}
"/" -> operation { a,b -> a+b}
else -> token.toInt()
}
}
return stack.removeLast()
}
fun evalRPN(tokens:Array<Int>): Int {
var stack = ArrayDeque<Int>()
for(token in tokens) {
when (token) {
"+" -> {
val a = stack.removeLast()
val b = stack.removeLast()
stack += b+a
}
"-" -> {
val a = stack.removeLast()
val b = stack.removeLast()
stack += b-a
}
"*" -> {
val a = stack.removeLast()
val b = stack.removeLast()
stack += b*a
}
"/" -> {
val a = stack.removeLast()
val b = stack.removeLast()
stack += b/a
}
else -> stack += token.toInt()
}
}
return stack.removeLast()
}
fun evalRPN(tokens: Array<String>): Int {
val stack = LinkedList<String>()
tokens.forEach {
if (it.isOperator()) {
val num1 = stack.pop()
val num2 = stack.pop()
when (it) {
"+" -> stack.push((num1.toInt() + num2.toInt()).toString())
"-" -> stack.push((num2.toInt() - num1.toInt()).toString())
"/" -> stack.push((num2.toInt() / num1.toInt()).toString())
"*" -> stack.push((num2.toInt() * num1.toInt()).toString())
}
} else
stack.push(it)
}
return stack.pop().toInt()
}
fun String.isOperator(): Boolean {
return this == "+" || this == "-" || this == "*" || this == "/"
}
fun resolves(a: Long, b: Long, Operator: Char): Long {
return when (Operator) {
'+' -> a + b
'-' -> a - b
'*' -> a * b
else -> a / b
}
}
fun evalRPN(tokens: Array<String>): Int {
val stack = Stack<Long>()
val n = tokens.size
for (i in 0..<n) {
if (tokens[i].length == 1 && tokens[i][0].code < 48) {
val integer2 = stack.pop()
val integer1 = stack.pop()
val operator = tokens[i][0]
val resolvedAns = resolves(integer1, integer2, operator)
stack.push(resolvedAns)
} else {
stack.push(tokens[i].toLong())
}
}
return stack.pop().toInt()
}
fun evalRPN(tokens: Array<String>): Int {
val stack = IntArray(tokens.size)
var top = 0
for (s in tokens) {
val c = s[0]
if (c == '+') {
val b = stack[--top]
val a = stack[--top]
stack[top++] = a + b
} else if (c == '-' && s.length == 1) {
val b = stack[--top]
val a = stack[--top]
stack[top++] = a - b
} else if (c == '*') {
val b = stack[--top]
val a = stack[--top]
stack[top++] = a * b
} else if (c == '/') {
val b = stack[--top]
val a = stack[--top]
stack[top++] = a / b
} else stack[top++] = s.toInt()
}
return stack[0]
}
fun evalRPN(tokens: Array<String>): Int {
val st = Stack<String>()
for (str in tokens) {
if (isOperator(str)) {
var result = 0
val ele2 = st.pop().toInt()
val ele1 = st.pop().toInt()
when (str) {
"+" -> result = ele1 + ele2
"-" -> result = ele1 - ele2
"/" -> result = ele1 / ele2
"*" -> result = ele1 * ele2
}
st.push(result.toString() + "")
} else {
st.push(str)
}
}
return st.peek().toInt()
}
private fun isOperator(str: String): Boolean {
return str == "+" || str == "-" || str == "*" || str == "/"
}