Leetcode#150 Evaluate Reverse Polish Notation
基本栈操作。
注意数字有可能是负的。
代码:
- int toInteger(string &s) {
- int res = ;
- bool negative = s[] == '-' ? true : false;
- for (int i = negative ? : ; i < s.length(); i++) {
- res *= ;
- res += s[i] - '';
- }
- return negative ? -res : res;
- }
- int compute(int a, int b, string sign) {
- switch (sign[]) {
- case '+':
- return a + b;
- case '-':
- return a - b;
- case '*':
- return a * b;
- case '/':
- return a / b;
- default:
- return -;
- }
- }
- int evalRPN(vector<string> &tokens) {
- stack<int> st;
- for (auto t : tokens) {
- if (t.length() > || t[] >= '' && t[] <= '')
- st.push(toInteger(t));
- else {
- int b = st.top();
- st.pop();
- int a = st.top();
- st.pop();
- st.push(compute(a, b, t));
- }
- }
- return st.top();
- }
Leetcode#150 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 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 解题报告(Python)
[LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...
- 150. Evaluate Reverse Polish Notation - LeetCode
Question 150. Evaluate Reverse Polish Notation Solution 2 1 + 3 * 是((2+1)*3)的后缀(postfix)或逆波兰(reverse ...
- 【LeetCode】150. Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
- 【刷题-LeetCode】150 Evaluate Reverse Polish Notation
Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...
随机推荐
- Web前端性能优化的9大问题
1.请减少HTTP请求基本原理:在浏览器(客户端)和服务器发生通信时,就已经消耗了大量的时间,尤其是在网络情况比较糟糕的时候,这个问题尤其的突出.一个正常HTTP请求的流程简述:如在浏览器中输入&qu ...
- 为hbase新增节点
为hbase增加新的节点,首先要为hadoop增加新新街点.因为我的做法是将datanode和regionserver放到一台物理机上.因此大体流程是: 1.克隆已经存在的regionserver虚拟 ...
- CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙。
firewall:systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service#停止firewallsyst ...
- 包装类(Wrapper Class)
1)包装类.针对于原生数据类型的包装.所有的包装类(8个)对位于java.lang包下.java中的8个包装类分别是:Byte,Short,Integer,Long,Float.Double,Char ...
- String类详解,StringBuffer
先说一下String类的equals()方法. 下面我们先看一段代码: 这段代码输出的结果为: ture true -------------- false 咋看之下貌似Object类比较特别,那么我 ...
- ORA-08189
OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...
- 关于生成缩略图及水印图片时出现GDI+中发生一般性错误解决方法
System.Drawing.Image OldImage = null; oldImage = System.Drawing.Image.FromFile(ImageUrl); 使用该方法读取图片时 ...
- sed实例一则
1.背景: test.txt文件里有这些语句 li^E1026^D20150802B07QH800^B698.^C20150801B08CDP00^B514.^C20150803D00A8L00^B2 ...
- JavaScript高级程序设计之函数
函数实际上是对象,每个函数都是Function类型的实例. 函数是引用类型. 函数名实际上是一个指向函数对象的指针,不会与某个函数绑定. // 这种写法更能表达函数的本质 var sum = func ...
- Windows Phone中In-App Purchase应用内购买
前言 应用内购买(In-App Purchase)对于开发者来说绝对是一个非常重要的功能,它提供了一个便捷的入口供用户来购买付费.在IAP盛行之前的游戏运营商一般都是通过接入第三方支付入口 ...