Problem
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Note:
Division between two integers should truncate toward zero.
The given RPN expression is always valid. That means the expression would always evaluate to a result and there won"t be any divide by zero operation.
Example 1:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9
Example 2:
Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6
Example 3:
Input: ["10", "6", "9", "3", "+", "-11", "", "/", "", "17", "+", "5", "+"]
Output: 22
Explanation:
((10 (6 / ((9 + 3) -11))) + 17) + 5
= ((10 (6 / (12 -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
class Solution { public int evalRPN(String[] tokens) { Dequestack = new ArrayDeque<>(); for (String token: tokens) { if (token.equals("+")) { stack.push(stack.pop()+stack.pop()); } else if (token.equals("-")) { stack.push(-stack.pop()+stack.pop()); } else if (token.equals("*")) { stack.push(stack.pop()*stack.pop()); } else if (token.equals("/")) { int divisor = stack.pop(); int dividend = stack.pop(); stack.push(dividend/divisor); } else { stack.push(Integer.parseInt(token)); } } return stack.pop(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72674.html
摘要:我們一般看到的數(shù)學(xué)表達(dá)式就是中綴表達(dá)式,也就是將符號(hào)放在兩個(gè)數(shù)字之間。后綴表達(dá)式也就是將運(yùn)算符放在相應(yīng)數(shù)字的后面。后綴表達(dá)式相當(dāng)于樹中的后序遍歷。通過獲得對(duì)應(yīng)位置的操作符。如果對(duì)應(yīng)的還是操作符,則繼續(xù)遞歸往前計(jì)算。 題目要求 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid...
摘要:題目鏈接來做,保存數(shù)字,碰到符號(hào)的時(shí)候就彈出兩個(gè)數(shù)字計(jì)算算完后再放入,最后里面的就是結(jié)果。 150. Evaluate Reverse Polish Notation 題目鏈接:https://leetcode.com/problems... stack來做,保存數(shù)字,碰到符號(hào)的時(shí)候就彈出兩個(gè)數(shù)字計(jì)算算完后再放入stack,最后stack里面的就是結(jié)果。 public class So...
摘要:題目根據(jù)逆波蘭表示法,求表達(dá)式的值。給定逆波蘭表達(dá)式總是有效的。逆波蘭表達(dá)式又叫做后綴表達(dá)式。解題思路可以看出逆波蘭表達(dá)式中的每一個(gè)運(yùn)算符屬于該運(yùn)算符前的兩個(gè)數(shù)字間的運(yùn)算。如如波蘭表達(dá)式則加號(hào)前兩個(gè)數(shù)字為。 題目: 根據(jù)逆波蘭表示法,求表達(dá)式的值。 有效的運(yùn)算符包括 +, -, *, / 。每個(gè)運(yùn)算對(duì)象可以是整數(shù),也可以是另一個(gè)逆波蘭表達(dá)式。 Evaluate the value of...
摘要:小鹿題目根據(jù)逆波蘭表示法,求表達(dá)式的值。給定逆波蘭表達(dá)式總是有效的。算法思路仔細(xì)觀察上述的逆波蘭表達(dá)式,可以發(fā)現(xiàn)一個(gè)規(guī)律就是每遇到一個(gè)操作符,就將操作符前的兩個(gè)操作數(shù)進(jìn)行運(yùn)算,將結(jié)果保存到原位置。 Time:2019/4/14Title: Evaluate Reverse Polish NotationDifficulty: MediumAuthor:小鹿 題目:Evaluate ...
摘要:棧法復(fù)雜度時(shí)間空間思路逆波蘭表達(dá)式的計(jì)算十分方便,對(duì)于運(yùn)算符,其運(yùn)算的兩個(gè)數(shù)就是這個(gè)運(yùn)算符前面的兩個(gè)數(shù)。注意對(duì)于減法,先彈出的是減號(hào)后面的數(shù)。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...
閱讀 2727·2021-11-22 15:22
閱讀 1631·2021-11-22 14:56
閱讀 3616·2021-09-22 15:12
閱讀 2403·2021-09-02 15:41
閱讀 2122·2021-08-27 16:26
閱讀 1113·2019-08-30 15:55
閱讀 2139·2019-08-29 17:30
閱讀 665·2019-08-29 16:26