博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 150:Evaluate Reverse Polish Notation
阅读量:4149 次
发布时间:2019-05-25

本文共 590 字,大约阅读时间需要 1 分钟。

Evaluate the value of an arithmetic expression in .

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

分析:

此题是逆波兰式的四则混合运算问题,算法如下:

1、构建空栈S。

2、遍历集合中得元素,如果是操作符op,则从栈中弹出两个元素S1, S2,计算S2 op S1的结果。

如果是操作数,则将其压入到栈S中。

代码如下,运算时间大约84ms。

class Solution {public:    int evalRPN(vector
&tokens) {
if(tokens.size() == 0) return 0; string op[] = {"+", "-", "*", "/"}; stack
mystack; map
op_map; for (int i = 0; i

转载地址:http://ryxti.baihongyu.com/

你可能感兴趣的文章
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python实现100以内自然数之和,偶数之和
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
pytorch
查看>>
pytorch(三)
查看>>
C++ 调用json
查看>>
动态库调动态库
查看>>
Kubernetes集群搭建之CNI-Flanneld部署篇
查看>>
k8s web终端连接工具
查看>>
手绘VS码绘(一):静态图绘制(码绘使用P5.js)
查看>>
手绘VS码绘(二):动态图绘制(码绘使用Processing)
查看>>
基于P5.js的“绘画系统”
查看>>
《达芬奇的人生密码》观后感
查看>>
论文翻译:《一个包容性设计的具体例子:聋人导向可访问性》
查看>>
基于“分形”编写的交互应用
查看>>
《融入动画技术的交互应用》主题博文推荐
查看>>