题目: Evaluate Reverse Polish Notation

Evaluatethe value of an arithmetic expression in Reverse Polish Notation. 

Valid operators are +, -, *, /. Each operand may be an integer or another expression. 

Some examples:

  ["", "", "+", "", "*"] -> (( + ) * ) ->
["", "", "", "/", "+"] -> ( + ( / )) ->

简单题,借助一个stack就可以实现,将数值入栈,遇操作符时将两个数值出栈,计算后再入栈,如此即可实现。

int evalRPN(vector<string> &tokens)
{
stack<int> stokens;
for (int i = ; i < tokens.size(); ++ i) {
if (tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/") {
int x1 = stokens.top();
stokens.pop();
int x2 = stokens.top();
stokens.pop();
int x3 = caculate(x1, x2, tokens[i]); //计算之后再入栈
stokens.push(x3);
}
else {
stokens.push(atoi(tokens[i].c_str()));
}
}
return stokens.top();
}
int caculate(int x1, int x2, string s)
{
int result;
if (s == "+")
result = x1 + x2;
else if (s == "-")
result = x1 - x2;
else if (s == "*")
result = x1 * x2;
else {
if (x1 >= x2)
result = x1 / x2;
else
result = x2 / x1;
}
return result;
}

LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium的更多相关文章

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

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

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

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

  3. LeetCode OJ:Evaluate Reverse Polish Notation(逆波兰表示法的计算器)

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

  4. LeetCode150_Evaluate Reverse Polish Notation评估逆波兰表达式(栈相关问题)

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

  5. [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式

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

  6. [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)

    原题链接:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题目描述: Evaluate the value of a ...

  7. [leetcode]Evaluate Reverse Polish Notation @ Python

    原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ 题意: Evaluate the value of an ...

  8. [LeetCode] Evaluate Reverse Polish Notation [2]

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

  9. Leetcode Evaluate Reverse Polish Notation

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

随机推荐

  1. oracle数据库分页总结

    /* BEGIN CREATE TABLE APPUSER(IDS NUMBER(8), USERNAME VARCHAR2(20), PASSWORD VARCHAR2(20), CTIME DAT ...

  2. c++ 面试题(网络类)

    1,若服务器方单独终止之后,客户端继续发数据会怎么样? https://blog.csdn.net/Nick_666/article/details/78342442 https://www.zhih ...

  3. mysql学习笔记--列属性

    一.是否为空----null || not null 二.默认值----default 三.自动增长----auto_increment 四.主键----primary key 1. 主键:唯一标识表 ...

  4. ASP 错误捕捉,处理

    Asp利用 On Error Resume Next捕捉异常,根据Err.Number判断是否有错误 注:On Error Goto 0取消捕捉异常 模板文件页面 <% Response.Buf ...

  5. python中的字符串

    一.在python中,字符串是不可变类型 通过以下代码说明: >>> s = 'hello, world' >>> id(s) 2108634288304 > ...

  6. AltiumDesigner 常用快捷键小结

    Ctrl + o  |  打开文件夹/文档 Ctrl + p  |  打印设置 Esc   |  从当前步骤退出 Shift +鼠标滚轮  |  向左/向右移动 Ctrl + C (或 Ctrl + ...

  7. docker上安装elasticsearch和ik分词器插件和header,实现分词功能

    docker run -di --name=tensquare_es -p 9200: -p 9300:9300 elasticsearch:5.6.8 创建elasticsearch容器(如果版本不 ...

  8. Pay attention to "Changing"

    data l_ct_imseg type vsep_t_imseg. refresh l_ct_imseg. append lines of ct_imseg to l_ct_imseg. call ...

  9. java命令分析线程死锁以及内存泄漏

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  10. Quartz.Net进阶之二:关于触发器的更多信息

    与作业一样,触发器相对容易使用,但是在您可以充分利用Quartz.NET之前,确实需要了解和理解各种可自定义的选项. 此外,如前所述,您可以选择不同类型的触发器来满足不同的调度需求. 1.常见触发器属 ...