国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] 150. Evaluate Reverse Polish Notation

KoreyLee / 2365人閱讀

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

Solution
class Solution {
    public int evalRPN(String[] tokens) {
        Deque stack = 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

相關(guān)文章

  • leetcode150. Evaluate Reverse Polish Notation

    摘要:我們一般看到的數(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...

    bitkylin 評(píng)論0 收藏0
  • 150. Evaluate Reverse Polish Notation

    摘要:題目鏈接來做,保存數(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...

    yanbingyun1990 評(píng)論0 收藏0
  • LeetCode 150:逆波蘭表達(dá)式求值 Evaluate Reverse Polish Nota

    摘要:題目根據(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...

    Turbo 評(píng)論0 收藏0
  • LeetCode 之 JavaScript 解答第150題 —— 逆波蘭表達(dá)式求值

    摘要:小鹿題目根據(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 ...

    104828720 評(píng)論0 收藏0
  • [Leetcode] Evaluate Reverse Polish Notation 計(jì)算逆波蘭表

    摘要:棧法復(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...

    ephererid 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<