Skip to content

Eason0814 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
edit 150
  • Loading branch information
xdongyan committed Aug 13, 2017
commit 99362c134bee2a5f2325450f1d8e946cb1565f6e
26 changes: 26 additions & 0 deletions src/main/java/com/fishercoder/solutions/_150.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,30 @@ public int evalRPN(String[] tokens) {
return Integer.parseInt(stack.pop());
}

//using one stack only.
public int evalRPN1(String[] tokens) {
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < tokens.length; i++) {
if (tokens[i].equals("+")){
stack.push(stack.pop() + stack.pop());
}
else if (tokens[i].equals("-")){
stack.push(-stack.pop() + stack.pop());
}
else if (tokens[i].equals("*") ){
stack.push(stack.pop() * stack.pop());

}
else if (tokens[i].equals("/")){
int a = stack.pop();
int b = stack.pop();
stack.push(b / a);

} else {
stack.push(Integer.parseInt (tokens[i]));
}
}
return stack.pop();
}

}