【LeetCode练习题】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic 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
题目意思:
计算出波兰计数法的结果。 解题思路:
利用一个栈,遇到数字就压栈,遇到符号就弹出两个数字,计算结果再push到栈里去。
注意几个函数就行了:isdigit()、stoi()。 代码如下:
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> ret;
int len = tokens.size();
for(int i = ; i < len; i++){
if( isdigit(tokens[i][]) || tokens[i].size() > ){
//如果是负数,第一个就是符号。
ret.push(stoi(tokens[i]));
}
else{
int op2 = ret.top();
ret.pop();
int op1 = ret.top();
ret.pop();
//注意除法和减法顺序,是用后pop出来的减去先pop出来的
switch(tokens[i][]){
case '+':
ret.push(op1 + op2);
break;
case '-':
ret.push(op1 - op2);
break;
case '*':
ret.push(op1 * op2);
break;
case '/':
ret.push(op1 / op2);
break;
}
}
}
return ret.top();
}
};
【LeetCode练习题】Evaluate Reverse Polish Notation的更多相关文章
- 【leetcode】Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation 题目描述: Evaluate the value of an arithmetic expression in Reverse Pol ...
- leetcode - [2]Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Total Accepted: 24595 Total Submissions: 123794My Submissions Evalu ...
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
一.问题描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 【leetcode】Evaluate Reverse Polish Notation(middle)
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- 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逆波兰表示法
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
- LeetCode——150. Evaluate Reverse Polish Notation
一.题目链接:https://leetcode.com/problems/evaluate-reverse-polish-notation/ 二.题目大意: 给定后缀表达式,求出该表达式的计算结果. ...
- Leetcode#150 Evaluate Reverse Polish Notation
原题地址 基本栈操作. 注意数字有可能是负的. 代码: int toInteger(string &s) { ; ] == '-' ? true : false; : ; i < s.l ...
随机推荐
- 三 ICE开发初级研究
http://www.acejoy.com/bbs/viewthread.php?tid=2878&extra=page%3D1 ICE开发初级研究(一) 最近一段一直在忙于工作,事情比较多, ...
- Search Insert Position 解答
Question Given a sorted array and a target value, return the index if the target is found. If not, r ...
- vi常用命令笔记
1.Vi 删除全部内容,删除某行到结尾,删除某段内容 (1)转到文件指定行 nG (2)删除所有内容(先用G转到文件尾) ,使用: $G :1,.d (3)删除第9行到第200行的内容(先用200G转 ...
- Oracle学习笔记(1)——查询及删除重复数据
1.查找表中多余的重复记录(根据单个字段studentid) select * from table_name where studentid in (select studentid fro ...
- module require区别
LUA modue require package 区别 2011-01-19 12:41:35| 分类: 默认分类 | 标签:lua package module require 加载 ...
- 使用react-native做一个简单的应用-01项目介绍
学习react-native也有一个月的时间了.当学习了关于react-native的基础知识之后,打算自己去仿一个应用去练手.于是花了10天左右的时间,这个小应用的基本功能也实现的差不多了. 在展示 ...
- React-Native获取文本框的值
要想获取文本框的值,首先我们需要看一下官方文档的解释: 这里的意思是说当文本框的内容改变的时候,文本框的输入的内容就会作为一个参数进行传递.因此我们就可以获取到文本框里面的内容就好了. constru ...
- XML Dtd Schema
在XML技术里,可以编写一个文档来约束一个XML文档的书写规范,这称之为XML约束. 整体比较: XML Schema符合XML语法结构. DOM.SAX等XML API很容易解析出XML Schem ...
- auto 和 decltype (C++11 新增)
红色字体为个人推断,可信度自辨. 蓝色字体为重点. auto类型说明符:使用auto时,编译器会分析表达式,并自动推算出变量所属类型.*auto变量必须有初值 原理:编译器通过 初值 来判断auto变 ...
- JS 中刷新页面的方法
整理了就是这几种,,有些在IE下面是不支持的,慎用... 1,history.go(0) 2,location.reload() 3,location=location 4,location.assi ...