1. 程式人生 > >Leetcode 150 Evaluate Reverse Polish Notation(棧)

Leetcode 150 Evaluate Reverse Polish Notation(棧)

解題思路:碰到數字入棧,碰到運算子,推出棧頂兩個元素進行相應運算,將結果放回棧中。

class Solution {
	public:
		int evalRPN(vector<string>& tokens) {
			stack<int> nums;
			for (int i = 0; i < tokens.size(); i++) {
				if (tokens[i] == "+" || tokens[i] == "-"
						|| tokens[i] == "/" || tokens[i] == "*") {
					int a = nums.top(); nums.pop();
					int b = nums.top(); nums.pop();
					if (tokens[i] == "+") nums.push(b + a);
					else if(tokens[i] == "-") nums.push(b - a);
					else if (tokens[i] == "*") nums.push(b * a);
					else if (tokens[i] == "/") nums.push(b / a);
				} else {
					nums.push(stoi(tokens[i]));
				}
			}
			return nums.top();
		}
};

相關推薦

Leetcode 150 Evaluate Reverse Polish Notation

解題思路:碰到數字入棧,碰到運算子,推出棧頂兩個元素進行相應運算,將結果放回棧中。class Solution { public: int evalRPN(vector<string>

[LeetCode] 150. Evaluate Reverse Polish Notation Java

value class 題目 als eval 註意 light 程序 highlight 題目: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operat

leetcode 150. Evaluate Reverse Polish Notation 逆波蘭表達式的計算

tm4 csv ref left ade 計算 cow wow tar J抗50俳k7甘2zp杜沃2http://shufang.docin.com/itdc957 嗇耪不侍40加怨8侔mcshttp://shequ.docin.com/igxg437 墓yT曰啡7I6

LeetCode 150. Evaluate Reverse Polish Notation

post ati spa solution 依次 進行 代碼 exp som Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,

[leetcode]150. Evaluate Reverse Polish Notation逆波蘭表示法

urn tco ssi his leet tput orm spa help Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +,

#Leetcode# 150. Evaluate Reverse Polish Notation

https://leetcode.com/problems/evaluate-reverse-polish-notation/   Evaluate the value of an arithmetic expression in Reverse Polish Notation. V

Leetcode [150. Evaluate 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

題目連結;https://leetcode.com/problems/evaluate-reverse-polish-notation/ 要求逆波蘭序(Reverse Polish Notation)的四則運算表示式的值,逆波蘭序的好處是不像我們手寫順序需要加括號以區分運算優先順序,所以逆波蘭序

LeetCode-150-Evaluate Reverse Polish Notation

tput put tar res top value divide ref alua 算法描述: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato

LeetCode: Evaluate Reverse Polish Notation計算逆波蘭表示式兩種方法

題目描述Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integ

LeetCode | Evaluate Reverse Polish Notation逆波蘭式求值

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an int

150. Evaluate Reverse Polish Notation

pre int isdigit lua cnblogs integer urn bsp a* Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators

150. Evaluate Reverse Polish Notation(逆波蘭表達式)

逆波蘭 逆波蘭表達式 spa rpn href self desc quest tor Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operator

150 Evaluate Reverse Polish Notation 逆波蘭表達式求值

div tps pro pre public res desc ever esc 求在 逆波蘭表示法 中算術表達式的值。有效的運算符號包括 +, -, *, / 。每個運算對象可以是整數,也可以是另一個逆波蘭計數表達。例如: ["2", "1", "+", "3", "*

leetcodeEvaluate Reverse Polish Notation

問題描述如下: Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. E

LeetCodeEvaluate Reverse Polish Notation 解題報告

【題意】 計算逆波蘭表示式(又叫字尾表示式)的值。 例如: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5"

Leetcode - Evaluate Reverse Polish Notation

++ urn == article ret lee ng- ring reverse 初看貌似有點復雜,可是搞懂了很easy,就一個簡單的棧應用,每次遇到計算符號"+", "-", "*", "/"就將棧頂端兩個數字出棧,計算後再將結果壓棧就可以。。 #includ

leetcode evaluate-reverse-polish-notation

evaluate expr i++ default 大神 cat nbsp java des 題目描述 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid o

224/227/772/150 Basic Calculator 1 2 3 and Evaluate Reverse Polish Notation

1. https://leetcode.com/problems/basic-calculator/description/ 2. https://leetcode.com/problems/basic-calculator-ii/description/ 3. https://lee

LeetCode150.逆波蘭表示式求解Evaluate Reverse Polish Notation

題目描述 根據逆波蘭表示法,求表示式的值。 有效的運算子包括 +, -, *, / 。每個運算物件可以是整數,也可以是另一個逆波蘭表示式。 說明: 整數除法只保留整數部分。 給定逆波蘭表示式總是有效的。換句話說,表示式總會得出有效數值且不存在除數為 0 的情況。