## LeetCode_150evalRPN [Evaluate Reverse Polish No..] 原

貉子

``````#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
using namespace std;

class Solution {
public:
// 参考：http://zh.wikipedia.org/wiki/逆波兰表示法 即可
int evalRPN(vector<string> &tokens) {
stack<int> stk;
for (int i = 0; i < tokens.size(); ++i) {
string word = tokens[i];
char ch = word[0];
if (word.size() > 1 || isdigit(ch))
stk.push(str2int(word));
else {
int val2 = stk.top(); stk.pop();
int val1 = stk.top(); stk.pop();
if (ch == '+') stk.push(val1 + val2);
else if (ch == '-') stk.push(val1 - val2);
else if (ch == '*') stk.push(val1 * val2);
else if (ch == '/') stk.push(val1 / val2);
}
}
return stk.top();
}

int str2int(string& s) {
int res = 0, flag = 1, i = 0;
if (!isdigit(s[0])) {
flag = s[0] == '+' ? 1 : -1;
++i;
}
for (; i < s.size(); ++i)
res = res * 10 + (s[i] - '0');
return res*flag;
}
};``````

### 貉子

150. Evaluate Reverse Polish Notation - LeetCode

Question 150. Evaluate Reverse Polish Notation Solution 是的后缀(postfix)或逆波兰(reverse Polish)记法，计算这个表达式容易想到栈，当见到一个数时就入栈，见到操作符时该运算符作用于...

yysue
2018/06/08
43
0
LeetCode 分类刷题—— Stack

07/04
0
0

2017/11/07
3
0
Reverse Nodes in k-Group

[LeetCode] 025. Reverse Nodes in k-Group (Hard) (C++/Java)，有需要的朋友可以参考下。 索引：[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/l......

panpan123_
2017/01/16
0
0

2-1: Each of the following things may be an atom, a list, or neither. Identify each accordingly. ATOM (THIS IS AN ATOM) (THIS IS AN EXPRESSION) ((A B) (C D) 3 (3) (LIST 3) (/ (+......

2012/01/18
137
0

PostgreSQL 11.3 locking

rudi

5
0
Mybatis Plus sql注入器

10
1

4
0

8
0

81
0