150. Evaluate Reverse Polish Notation

原创 2016年05月30日 10:35:52

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
题意:逆波兰式的运算结果。

思路:用栈实现。仿佛回到了,大二刚开学时的数据结构实验。简易版计算器的编写。

class Solution {
public:
	int evalRPN(vector<string>& tokens) {
		stack<int> mystack;
		for (int i = 0; i < tokens.size(); i++){
			string s = tokens[i];
			if (s.compare("+") == 0){
				int a = mystack.top();
				mystack.pop();
				int b = mystack.top();
				mystack.pop();
				mystack.push(a + b);
				continue;
			}
			if (s.compare("-") == 0){
				int a = mystack.top();
				mystack.pop();
				int b = mystack.top();
				mystack.pop();
				mystack.push(b - a);
				continue;
			}
			if (s.compare("*") == 0){
				int a = mystack.top();
				mystack.pop();
				int b = mystack.top();
				mystack.pop();
				mystack.push(a * b);
				continue;
			}
			if (s.compare("/") == 0){
				int a = mystack.top();
				mystack.pop();
				int b = mystack.top();
				mystack.pop();
				mystack.push(b / a);
				continue;
			}
			mystack.push(atoi(s.c_str()));
		}
		return mystack.top();
	}
};










版权声明:本文为博主原创文章,未经博主允许不得转载。

150. Evaluate Reverse Polish Notation

Problem Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid ope...

LeetCode 150 — Evaluate Reverse Polish Notation(C++ Java Python)

题目:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ Evaluate the value of an arithm...

LeetCode 150. Evaluate Reverse Polish Notation 辅助数据结构栈

题目 题意 注意 思路 代码 结果题目 Evaluate the value of an arithmetic expression in Reverse Polish Notation. ...

[leetcode] 150.Evaluate Reverse Polish Notation

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

Leetcode 150:Evaluate Reverse Polish Notation(计算逆波兰表达式) --java实现

1.概念 中缀表达式:1+2*3-4;4+5*1-6;平常我们所使用到的这种表达式,操作符在操作数的中间,这种表达式称为中缀表达式 后缀表达式:后缀表达式又叫逆波兰表达式,是为了纪念波兰的一位数学...

LeetCode:150. Evaluate Reverse Polish Notation(逆波兰表达式)

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

Leetcode 150 Evaluate Reverse Polish Notation (求值逆波兰表达式)

一,问题描述 1,用逆波兰表达式法去计算算术表达式的值。其中,有效运算符为+,-,*,/。每个操作数可以是整数或者另一个表达式2,例如: 3,解题思路: 先定义一个栈用来存放数据的,然后对字...

Leetcode 150:Evaluate Reverse Polish Notation

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

LeetCode 150 Evaluate Reverse Polish Notation

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

Evaluate Reverse Polish Notation - LeetCode 150

题目描述: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators...
  • bu_min
  • bu_min
  • 2015年05月28日 22:50
  • 212
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:150. Evaluate Reverse Polish Notation
举报原因:
原因补充:

(最多只允许输入30个字)