LeetCode第一题:Evaluate Reverse Polish Notation

时间:2014.06.11

地点:基地

--------------------------------------------------------------------

一、题目

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", "*"] -> ((2 + 1) * 3) -> 9
  ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6

--------------------------------------------------------------------

二、逆波兰求值问题,直接上源码

注意除法不满足交换律

#include<vector>
#include<stack>
#include<string>
#include<cctype>
#include<iostream>
using namespace std;
class Solution {
public:
	int evalRPN(vector<string> &tokens) {
		stack<int> my_stack_int;
		int lf = 0, rh = 0;
		for (auto& str : tokens)
		{
			if (isdigit(str[0]))
				my_stack_int.push(stoi(str));
			else
			{
				rh = my_stack_int.top();
				my_stack_int.pop();
				lf = my_stack_int.top();
				my_stack_int.pop();
				switch (str[0]){
				case '+':
					lf += rh;break;
				case '-':
					lf -= rh;break;
				case '/':
					lf /= rh;break;
				case '*':
					lf *= rh;break;
				default:
					break;
				}
				my_stack_int.push(lf);
			}
		}
		return my_stack_int.top();
	}
};
int main()
{
	vector<string> str_vec{ "4", "13", "5", "/", "+" };
	Solution sl;
	int a = sl.evalRPN(str_vec);
	cout << a << endl;
}

LeetCode第一题:Evaluate Reverse Polish Notation

时间: 2024-10-13 02:35:12

LeetCode第一题:Evaluate Reverse Polish Notation的相关文章

【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 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", "+&qu

【leetcode刷题笔记】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

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

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

[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

Evaluate Reverse Polish Notation leetcode 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 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