FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc150.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
code
/
lc150.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
41 lines (40 loc) · 1.25 KB
Breadcrumbs
leetcode
/
code
/
lc150.java
Copy path
File metadata and controls
41 lines (40 loc) · 1.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
code
;
/*
* 150. Evaluate Reverse Polish Notation
* 题意:波兰表达式运算
* 难度:Medium
* 分类:Stack
* 思路:用栈即可
* Tips:
*/
import
java
.
util
.
Stack
;
public
class
lc150
{
public
int
evalRPN
(
String
[]
tokens
) {
if
(
tokens
.
length
==
1
)
return
Integer
.
valueOf
(
tokens
[
0
]);
Stack
<
Integer
>
st
=
new
Stack
();
String
opration
=
"+-*/"
;
int
res
=
0
;
for
(
int
i
=
0
;
i
<
tokens
.
length
;
i
++) {
if
(
opration
.
contains
(
tokens
[
i
])){
int
n2
=
st
.
pop
();
//注意n1,n2顺序,除法时两个数是有顺序的
int
n1
=
st
.
pop
();
System
.
out
.
println
(
n1
);
System
.
out
.
println
(
n2
);
if
(
tokens
[
i
].
equals
(
"+"
)){
//注意用equeals
res
=
n1
+
n2
;
}
else
if
(
tokens
[
i
].
equals
(
"-"
)){
res
=
n1
-
n2
;
}
else
if
(
tokens
[
i
].
equals
(
"*"
)){
res
=
n1
*
n2
;
}
else
if
(
tokens
[
i
].
equals
(
"/"
)){
res
=
n1
/
n2
;
}
st
.
push
(
res
);
System
.
out
.
println
(
res
);
}
else
{
st
.
push
(
Integer
.
valueOf
(
tokens
[
i
]));;
}
}
return
res
;
}
}
Back
|
FazBrowse Home
|
New Git URL