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.逆波兰表达式求值
逆波兰表达式求值 根据逆波兰表示法,求表达式的值. 有效的运算符包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰表达式. 说明: 整数除法只保留整数部分. 给定逆波兰表达式总 ...
随机推荐
- 【ASP.Net】publish asp.net to local IIS
做web项目难免要将项目部署, 要么部署在azure上,要么部署在本地, 使用IIS去host. 部署步骤很简单, 1. vs打开你的web项目, 项目名上面右键选择publish 2. 在弹出的pu ...
- VHDL 数字时钟设计
序言 这个是我在做FPGA界的HelloWorld--数字钟设计时随手写下的,再现了数字钟设计的过程 目标分析 时钟具有时分秒的显示,需6个数码管.为了减小功耗采用扫描法显示 按键设置时间,需要对按键 ...
- MS-Windows中的Git命令行
Git command line for MS-Windows Inhalt 1 Download and install, or copy the git command line suite fo ...
- 一、python (int & str 的方法)
1.变量:命名与使用 #!/usr/bin/env/ python # -*- coding:utf-8 -*- name = 'liQM' 只能包含字母.数字或下划线: 第一个字符不能是数字: 简短 ...
- 原生js总结
数据类型 基本类型值包括: undefined,null,Boolean,Number和String,这些类型分别在内存中占有固定的大小空间,它们的值保存在栈空间,我们通过按值来访问的. 引用类型包括 ...
- openlayer ol.js和ol-debug.js的使用 调试技巧
二者实现的功能是一样的,有以下区别 : ol.js一般打包项目的时候使用, ol-debug.js编写代码调试的时候使用. 下边是用ol-debug.js编写代码调试的显示: 编写代码调试的技巧,所有 ...
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
- R的极客理想系列文章--转载
http://blog.fens.me/series-r/ R的极客理想系列文章,涵盖了R的思想,使用,工具,创新等的一系列要点,以我个人的学习和体验去诠释R的强大. R语言作为统计学一门语言,一直在 ...
- hdu 4349 Xiao Ming's Hope 规律
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 5676 ztr loves lucky numbers 打表+二分
ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...