Leetcode 150
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的更多相关文章
- LeetCode 150. 逆波兰表达式求值(Evaluate Reverse Polish Notation) 24
150. 逆波兰表达式求值 150. Evaluate Reverse Polish Notation 题目描述 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, /.每个运算对象 ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Java实现 LeetCode 150 逆波兰表达式求值
150. 逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波 ...
- Java for LeetCode 150 Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- leetcode 150. Evaluate Reverse Polish Notation ------ java
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
- [leetcode]150. Evaluate Reverse Polish Notation逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- Leetcode 150.逆波兰表达式求值
逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总 ...
随机推荐
- Vim-编辑器之神
几点声明: 作者只是一位小小的 \(OIer\) ,并不会什么过于神仙的东西,我这篇文章只是帮助人入门的而已. 若有人在 OI-Wiki上见过了原文章,原作者是我 \(......\) ,真不是抄 \ ...
- linux服务器安装brook服务端 使用brook客户端
既然你已经找到了此文章,说明已经知道brook的用途了,不做介绍,下面讲安装方法: 连接服务器,随便cd一个安装目录,例如: mkdir brook && cd brook 2.进re ...
- P1262 间谍网络
传送门 思路: ①在 Tarjan 的基础上加一个 belong 记录每个点属于哪个强连通分量. ②存图完成后,暴力地遍历全图,查找是否要间谍不愿受贿. inline void dfs(int u) ...
- Java一次性读取文件的内容
我们做文本处理的时候的最常用的就是读写文件了,尤其是读取文件,不论是什么文件,我都倾向于一次性将文本的原始内容直接读取到内存中再做处理,当然,这需要你有一台大内存的机器,内存不够者……可以一次读取少部 ...
- Spring 的@@Autowired 和 @Qualifier注释
@Autowired spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造方法进行标注,配合AutowiredAnnotationBeanProc ...
- Hexo 的next主题下添加网易云音乐作BGM
首先,你要看看你选中的歌能不能在网页版的网易云音乐生成外链,因为版权保护原因,有些音乐是生不成外链的,比如这样的: 所以,选些可以生成外链的音乐.生成对应的外链 比如这里的重点是HTML代码中的src ...
- MySQL学习(十六)
MySQL高级部分 触发器 触发器是一类特殊的事务,可以监视某种数据操作(insert/update/delete),并触发相关的操作(insert/update/delete) 触发器创建语法之4要 ...
- Codeforces 797A - k-Factorization
A. k-Factorization 题目链接:http://codeforces.com/problemset/problem/797/A time limit per test 2 seconds ...
- Server SQL Modes
The MySQL server can operate in different SQL modes, and can apply these modes differently for diffe ...
- tomcat ----> 源码关联/编译/....
今天在搞Servlet时想看下tomcat Servlet-API.jar的源码,按照惯性思维用以往关联SSH2框架的源码的方式去做但是结果没有成功,尝试过换不同的文件夹,起初怀疑可能是路径太深关联不 ...