forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_stack_n.java
43 lines (39 loc) · 1.17 KB
/
AC_stack_n.java
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_stack_n.java
* Create Date: 2015-03-15 16:13:17
* Descripton:
*/
import java.util.*;
public class Solution {
public int evalRPN(String[] tokens) {
if (tokens == null || tokens.length == 0) {
return 0;
}
String op = new String("+-*/");
Stack<Integer> stack = new Stack<Integer>();
for (String str : tokens) {
int pos = op.indexOf(str);
if (pos == -1) {
stack.push(Integer.parseInt(str));
} else {
int a = stack.pop();
int b = stack.pop();
switch (pos) {
case 0: stack.push(b + a); break;
case 1: stack.push(b - a); break;
case 2: stack.push(b * a); break;
case 3: stack.push(b / a); break;
}
}
}
return stack.peek();
}
// debug
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
Solution s = new Solution();
int[] input = {1, 2, 3, 4};
System.out.println("no case");
}
}