class Solution {
public:
int evalRPN(vector<string>& tokens) {
stack<int> st;
for(int i=;i < tokens.size();i++){
char temp = tokens[i][tokens[i].size()-];
if(temp >= '' && temp <= ''){
st.push(trams(tokens[i]));
}
else{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
int res = ;
if(tokens[i] == "+") res = a+b;
if(tokens[i] == "-") res = b-a;
if(tokens[i] == "*") res = a*b;
if(tokens[i] == "/") res = b/a;
st.push(res);
}
}
return st.top();
}
int trams(string s){
int res = ;
int flag = ;
int j = ;
if(s[] == '-'){
flag = ;
j = ;
}
for(int i=j;i < s.size();i++){
char temp = s[i];
res = res* + (temp-'');
}
if(flag == ) res = -res;
return res;
}
};

字符串string几大坑人之处:1. 必须要用char = s[0]才能进行比较和变数字(char - '0')

              2.s == " " (双冒号) s[0] == ' ' (单冒号)

Leetcode 150的更多相关文章

  1. LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24

    150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...

  2. LeetCode——150. Evaluate Reverse Polish Notation

    一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...

  3. [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  4. Java实现 LeetCode 150 逆波兰表达式求值

    150. 逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波 ...

  5. Java for LeetCode 150 Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  6. leetcode 150. Evaluate Reverse Polish Notation ------ java

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  7. Leetcode#150 Evaluate Reverse Polish Notation

    原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...

  8. [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...

  9. Leetcode 150.逆波兰表达式求值

    逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总 ...

随机推荐

  1. 1 --- Vue 基础指令

    1.vue 指令 1.v-model  主要在表单中使用,文本框.teaxare.单选.下拉 等 2.v-text   文本渲染  类似{{}} 3.v-show  控制Dom显示隐藏   displ ...

  2. C#解析html文档类库HtmlAgilityPack下载地址

    新:http://html-agility-pack.net/?z=codeplex 原:http://htmlagilitypack.codeplex.com/

  3. 【Selenium2】【Shell】

    E:\test_object>Python all_test.py >> report/log.txt 2>&1

  4. 小程序学习一 .json 文件配置

    微信小程序——配置 以下就是小编对小程序配置的资料进行的系统的整理,希望能对开发者有帮助. 我们使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置 ...

  5. P1005 矩阵取数游戏

    传送门 思路: △ 区间动规 对于每行,有 f [ i ][ j ] 代表取区间 [ i , j ] 的最大值. 然后转移方程我们考虑,对于每一个新的 f [ i ][ j ],有两种情况(下面定义  ...

  6. P2002 消息扩散

    其实这道题蛮水的 思路: 根据题意,他说有环,自然想到要用tarjan,后面就很简单了: 缩完点之后重新建图,开一个inin数组表示该点的入度是多少(psps:该点表示缩完点之后的大点): 最后统计一 ...

  7. 2.0 vue内置指令与自定义指令

    1.1 常用内置指令 1) v:text : 更新元素的 textContent 2) v-html : 更新元素的 innerHTML 3) v-if : 如果为 true, 当前标签才会输出到页 ...

  8. 6-1 建立客户端与zk服务端的连接

    6-1 建立客户端与zk服务端的连接 zookeeper原生java api使用 会话连接与恢复; 节点的增删改查; watch与acl的相关操作; 导入jar包;

  9. 转载 R语言颜色基础设置

    原文链接:http://www.biostatistic.net/thread-5065-1-1.html R语言在画图形的时候,经常遇到颜色设定问题,用户可以根据color.rgb值和hsv值来设定 ...

  10. 学习笔记25—python基本运算法则

    1.矩阵的点乘: a*b, 矩阵乘法:dot(a*b),矩阵的次方:a**num (num = 2,表示2次)2.数组的并集,交集: >>> a = [1,2,3] >> ...