leetcode栈--1、evaluate-reverse-polish-notation(逆波兰表达式)

题目描述

Evaluate the value of an arithmetic expression inReverse 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

解题思路:使用栈结构来存储。遇到数字存入栈中,遇到符号,弹出栈顶的两个元素,进行操作,并将结果放入栈中。

 1 class Solution {
 2 public:
 3     int strtoint(string s) // string转int
 4     {
 5         int result = 0;
 6         int base = 1;//记录属于的位
 7         int t = 1;//记录正负号
 8         if(s[0] == ‘-‘)
 9             t = -1;
10         for(int i= s.size() - 1;i>=0;i--)
11         {
12             if(s[i]>=‘0‘ && s[i]<=‘9‘)//此处一定注意0和9加‘‘
13             {
14                 result += base * (s[i] - ‘0‘);
15                 base *= 10;
16             }
17         }
18         return result*t;
19     }
20     int evalRPN(vector<string> &tokens) {
21         int n = tokens.size();
22         if(tokens.empty() || n<=0)
23             return 0;
24         stack<int> s;
25         for(int i=0;i<n;i++)
26         {
27             if(tokens[i] == "+" || tokens[i] == "-" || tokens[i] == "*" || tokens[i] == "/")
28             {
29                 int num2 = s.top();//先弹出的为右操作数
30                 s.pop();
31                 int num1 = s.top();
32                 s.pop();
33                 if(tokens[i] == "+")
34                 {
35                     s.push(num1+num2);
36                 }
37                 else if(tokens[i] == "-")
38                 {
39                     s.push(num1-num2);
40                 }
41                 else if(tokens[i] == "*")
42                 {
43                     s.push(num1*num2);
44                 }
45                 else
46                 {
47                     s.push(num1/num2);
48                 }
49             }
50             else
51             {
52                 s.push(strtoint(tokens[i]));
53             }
54         }
55         return s.top();
56     }
57 };
时间: 2024-10-12 17:43:03

leetcode栈--1、evaluate-reverse-polish-notation(逆波兰表达式)的相关文章

Java Evaluate Reverse Polish Notation(逆波兰表达式)

表达式:: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9   ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 题目大意:给定一个逆波兰表达式,求该表达式的值 思路:由于逆波兰表达式本身

lintcode 中等题:Evaluate Reverse Polish notation逆波兰表达式求值

题目 逆波兰表达式求值 在逆波兰表达法中,其有效的运算符号包括 +, -, *, / .每个运算对象可以是整数,也可以是另一个逆波兰计数表达. 样例 ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"]

150. 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", "*"] -&g

【leetcode】:Evaluate Reverse Polish Notation (python)

逆波兰式的求解,建立一个类栈容器,遍历给定的逆波兰表达式,遇到数字就push, 遇到操作符就进行出栈,连续出两次,因为给定的四则运算符都是双目的,这里注意下这两个操作数的先后顺序,因为对于加法和乘法没关系,但是对于减法和除法是有先后关系的.然后进行相应的运算,将结果push进栈中. 这里附带说明下python中进行除法运算与c,java系列中的除法的不同,就是向下取整的问题.这种不同表现在两个操作数符号不同时的情况. 在c 中 3 / -5 = 0,但是在python中, 结果却为 - 1.这种

Leetcode 448. 150. Evaluate Reverse Polish Notation JAVA语言

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", "*"] -

[LeetCode]Evaluate Reverse Polish Notation

题目:Evaluate Reverse Polish Notation 给出一个加减乘除的逆波兰式,求出它的结果: 什么是逆波兰式? 简单来说,逆波兰式就是表达式的后缀表示形式: 例如下面两个式子: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", &quo

[leetcode]Evaluate Reverse Polish Notation @ Python

原题地址:https://oj.leetcode.com/problems/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

LeetCode: Evaluate Reverse Polish Notation [150]

[题目] 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", "*"

LeetCode OJ——练习笔记(1)Evaluate Reverse Polish Notation

院招终于开始了,然后期待与兴奋过后却是面临着笔试一次又一次的失败,然后开始留意到LeetCode. 也想自己去体验一下诸多大牛通向无限coding路上都攻克过的一关. 话不多说,贴出原题: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expres

[LeetCode][JavaScript]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", "+",