Question

150. Evaluate Reverse Polish Notation

Solution

2 1 + 3 * ((2+1)*3)的后缀(postfix)或逆波兰(reverse Polish)记法,计算这个表达式容易想到栈,当见到一个数时就入栈,见到操作符时该运算符作用于从该栈中弹出的两个数上,将所得结果入栈。

public int evalRPN(String[] tokens) {
Stack<Integer> stack = new Stack<>();
for (String tmp : tokens) {
if (tmp.length() > 1) {
stack.push(Integer.parseInt(tmp));
continue;
}
char c = tmp.charAt(0); // String转char
int a, b;
switch (c) {
case '+':
b = stack.pop();
a = stack.pop();
stack.push(a + b);
break;
case '-':
b = stack.pop();
a = stack.pop();
stack.push(a - b);
break;
case '*':
b = stack.pop();
a = stack.pop();
stack.push(a * b);
break;
case '/':
b = stack.pop();
a = stack.pop();
stack.push(a / b);
break;
default:
stack.push(c - '0'); // char 转 int
}
}
return stack.pop();
}

Reference

150. Evaluate Reverse Polish Notation - LeetCode的更多相关文章

  1. 【LeetCode】150. Evaluate Reverse Polish Notation 解题报告(Python)

    [LeetCode]150. Evaluate Reverse Polish Notation 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/ ...

  2. 【LeetCode】150. Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

  3. 【刷题-LeetCode】150 Evaluate Reverse Polish Notation

    Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish No ...

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

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

  5. LeetCode OJ 150. Evaluate Reverse Polish Notation

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

  6. Java for LeetCode 150 Evaluate Reverse Polish Notation

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

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

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

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

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

  9. Evaluate Reverse Polish Notation——LeetCode

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

随机推荐

  1. SpringCloudAlibaba 微服务讲解(四)Sentinel--服务容错(二)

    4.7 Sentinel 规则 4.7.1 流控规则 流量控制,其原理是监控应用流量的QPS(每秒查询率)或并发线程数等指标,当达到指定的阈值时对流量进行控制,以避免被瞬时的榴莲高峰冲垮,从而保障应用 ...

  2. 寄存器、特殊功能寄存器和ram之间的区别联系

    存储器在CPU外,一般指硬盘,U盘等可以在切断电源后保存资料的设备,容量一般比较大,缺点是读写速度都很慢,普通的机械硬盘读写速度一般是50MB/S左右. 内存和寄存器就是为了解决存储器读写速度慢而产生 ...

  3. 《剑指offer》面试题4:替换空格

    面试题4:替换空格 题目:请实现一个函数,把字符串中的每个空格替换成"%20",例如输入"We are happy.",则输出"we%20are%20 ...

  4. 用原生js写一个"多动症"的简历

    用原生js写一个"多动症"的简历 预览地址源码地址 最近在知乎上看到@方应杭用vue写了一个会动的简历,觉得挺好玩的,研究一下其实现思路,决定试试用原生js来实现. 会动的简历实现 ...

  5. H5 视频播放解决方案

    前两天,美团推出的杨洋H5火爆朋友圈.里面主要的是多段视频播放.暂停.听起来很简单,但是由于腾讯白名单限制,在微信浏览器,qq浏览器,会自动将video标签中非腾讯域名的视频 ,自动全屏,结尾追加视频 ...

  6. 小程序wx.getRecorderManager()录音管理

    小程序中提供了两种录音的API,wx.startRecord和wx.getRecorderManager(),前一个现在微信团队已经不再维护,所以在这里写一下新的录音管理,比之前要强大 1.小程序录音 ...

  7. 关于websocket制作聊天室的的一些总结

    websocket的总结 在一个聊天室系统中,常常使用websocket作为通信的主要方式.参考地址:https://www.jianshu.com/p/00e... 关于自己的看法:websocke ...

  8. 前端面试题整理——手写方法解析URL参数

    //拆分字符串形式 function queryToObj() { const res = {} const search = location.search.substr(1);//去掉前面的&qu ...

  9. cisco packet tracer安装步骤

    一.进入Cisco Networking Academy Builds IT Skills & Education For Future Careers (netacad.com) 二.注册, ...

  10. mint-ui中messagebox的使用

    效果图: 代码: // 安装 # Vue 1.x npm install mint-ui@1 -S # Vue 2.0 npm install mint-ui -S // 引入全部组件 import ...