LeetCode:Everaluate Reverse Polish Notation[150]
【タイトル】
Evaalute the value of an arthmetic expression in Reverse Polish Notation.
Valid operators are
Some examples:
逆ポーランド式で表される表現の値を計算します.
【考え方】
逆ポーランド式は実は二叉樹の遍歴です. スタックで解決すればいいです.毎回演算子に出会うと、スタックの一番上の二つの要素を計算します. 注意: 文字列が整数を回転する場合、負の数を考慮します.
【コード】
Evaalute the value of an arthmetic expression in Reverse Polish Notation.
Valid operators are
+
、 -
、 *
、 /
.Each operand may be an integer or another expression.Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
【題意】逆ポーランド式で表される表現の値を計算します.
【考え方】
逆ポーランド式は実は二叉樹の遍歴です. スタックで解決すればいいです.毎回演算子に出会うと、スタックの一番上の二つの要素を計算します. 注意: 文字列が整数を回転する場合、負の数を考慮します.
【コード】
class Solution {
public:
int str2int(string token){
int num=0;
int start=0;
int isNev = 1;
//
if(token[0]=='-'){isNev=-1; start++;}
else if(token[0]=='+')start++;
for(int i=start; i &tokens) {
stack st;
for(int i=0; i