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

Commit48db8be

Browse files
author
applewjg
committed
EvaluateReversePolishNotation
Change-Id: I371af5e39ef9577537e5118f287d75f3eb003664
1 parent2c41774 commit48db8be

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎EvaluateReversePolishNotation.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Author: Andy, nkuwjg@gmail.com
3+
Date: Jan 31, 2015
4+
Problem: Evaluate Reverse Polish Notation
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
7+
Notes:
8+
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
9+
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
10+
Some examples:
11+
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
12+
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
13+
Solution: stack.
14+
*/
15+
16+
publicclassSolution {
17+
publicintevalRPN(String[]tokens) {
18+
Stack<Integer>stk =newStack<Integer>();
19+
for (inti =0;i <tokens.length; ++i) {
20+
if ((tokens[i].compareTo("+") !=0) && (tokens[i].compareTo("-") !=0)
21+
&& (tokens[i].compareTo("*") !=0) && (tokens[i].compareTo("/") !=0)) {
22+
stk.push(Integer.parseInt(tokens[i]));
23+
}else {
24+
intop2 =stk.pop();
25+
intop1 =stk.pop();
26+
intres =0;
27+
if(tokens[i].compareTo("+") ==0)
28+
res =op1 +op2;
29+
elseif(tokens[i].compareTo("-") ==0)
30+
res =op1 -op2;
31+
elseif(tokens[i].compareTo("*") ==0)
32+
res =op1 *op2;
33+
elseif(tokens[i].compareTo("/") ==0)
34+
res =op1 /op2;
35+
stk.push(res);
36+
}
37+
}
38+
returnstk.pop();
39+
}
40+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp