亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

[LintCode] Evaluate Reverse Polish Notation

luckyyulin / 2352人閱讀

摘要:中的運算符,運算之后要出來,繼續(xù)遍歷數(shù)組。是放入新的數(shù)字,用轉換為均可。

Problem

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

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Example
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
Note

Switch中的運算符case,運算之后要break出來,繼續(xù)遍歷tokens數(shù)組。
default是放入新的數(shù)字,用parseInt()/valueOf()轉換String為Integer均可。

Solution
public class Solution {
    public int evalRPN(String[] tokens) {
        if (tokens == null || tokens.length % 2 == 0) return 0;
        Stack stack = new Stack();
        for (String tok: tokens) {
            switch(tok) {
                case "+": 
                    stack.push(stack.pop() + stack.pop());
                    break;
                case "-":
                    stack.push(-stack.pop() + stack.pop());
                    break;
                case "*":
                    stack.push(stack.pop() * stack.pop());
                    break;
                case "/":
                    int divisor = stack.pop();
                    int dividend = stack.pop();
                    stack.push(dividend/divisor);
                    break;
                default:
                    stack.push(Integer.valueOf(tok));
            }
        }
        return stack.pop();
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://www.ezyhdfw.cn/yun/65499.html

相關文章

  • [Leetcode] Evaluate Reverse Polish Notation 計算逆波蘭表

    摘要:棧法復雜度時間空間思路逆波蘭表達式的計算十分方便,對于運算符,其運算的兩個數(shù)就是這個運算符前面的兩個數(shù)。注意對于減法,先彈出的是減號后面的數(shù)。 Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operato...

    ephererid 評論0 收藏0
  • [LeetCode] 150. Evaluate Reverse Polish Notation

    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 inte...

    KoreyLee 評論0 收藏0
  • 150. Evaluate Reverse Polish Notation

    摘要:題目鏈接來做,保存數(shù)字,碰到符號的時候就彈出兩個數(shù)字計算算完后再放入,最后里面的就是結果。 150. Evaluate Reverse Polish Notation 題目鏈接:https://leetcode.com/problems... stack來做,保存數(shù)字,碰到符號的時候就彈出兩個數(shù)字計算算完后再放入stack,最后stack里面的就是結果。 public class So...

    yanbingyun1990 評論0 收藏0
  • leetcode150. Evaluate Reverse Polish Notation

    摘要:我們一般看到的數(shù)學表達式就是中綴表達式,也就是將符號放在兩個數(shù)字之間。后綴表達式也就是將運算符放在相應數(shù)字的后面。后綴表達式相當于樹中的后序遍歷。通過獲得對應位置的操作符。如果對應的還是操作符,則繼續(xù)遞歸往前計算。 題目要求 Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid...

    bitkylin 評論0 收藏0
  • 表達式類算法題小結

    摘要:將表達式轉換為逆波蘭式,然后求值轉換中綴表達式為逆波蘭式魯棒性檢測,表達式中沒有操作數(shù)計算逆波蘭式值參考 表達式類算法題小結 [TOC] 聲明 文章均為本人技術筆記,轉載請注明出處:[1] https://segmentfault.com/u/yzwall[2] blog.csdn.net/j_dark/ 表達式分類 根據(jù)運算符與相關操作操作數(shù)的位置不同,將表達式分為前綴,中綴和后綴表...

    Heier 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<