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

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] 227. Basic Calculator II

silvertheo / 2147人閱讀

Problem

Implement a basic calculator to evaluate a simple expression string.

The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero.

Example 1:

Input: "3+2*2"
Output: 7

Example 2:

Input: " 3/2 "
Output: 1

Example 3:

Input: " 3+5 / 2 "
Output: 5

Note:

You may assume that the given expression is always valid.
Do not use the eval built-in library function.

Solution
class Solution {
    public int calculate(String s) {
        s = s.trim().replaceAll(" ", "");
        if (s == null || s.length() == 0) return 0;
        long pre = 0;
        int res = 0, i = 0;
        char sign = "+";
        
        while (i < s.length()) {
            long cur = 0;
            while (i < s.length() && Character.isDigit(s.charAt(i))) {
                cur = cur*10 + (s.charAt(i)-"0");
                i++;
            }
            switch (sign) {
                case "+":
                    res += pre;
                    pre = cur; break;
                case "-":
                    res += pre;
                    pre = -cur; break;
                case "*":
                    pre *= cur; break;
                case "/":
                    pre /= cur; break;
            }
            if (i < s.length()) {
                sign = s.charAt(i);
                i++;
            }
        }
        
        res += pre;
        return res;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/77154.html

相關(guān)文章

  • Leetcode[227] Basic Calculator II

    摘要:復(fù)雜度思路用兩個(gè)來(lái)分別記錄當(dāng)前的結(jié)果和操作符注意每一次統(tǒng)計(jì)當(dāng)前的的時(shí)候,要看一下下一位的操作符。有一種的方法,是表示的是匹配任意的空白符,包括空格,制表符,換行符,中文全角空格等。也可以用更簡(jiǎn)單的方法,。 LeetCode[227] Basic Calculator II Implement a basic calculator to evaluate a simple expres...

    chaos_G 評(píng)論0 收藏0
  • 224. Basic Calculator & 227. Basic Calculator

    摘要:題目鏈接,就是感覺(jué)條件有點(diǎn)多簡(jiǎn)單點(diǎn)的寫(xiě)法,把直接用存在里面,就存成,存成題目鏈接這題就是碰到在加減后面怎么處理的問(wèn)題。用一個(gè)來(lái)表示之前的,所以碰到現(xiàn)在是乘的時(shí)候就,除類(lèi)似。 224. Basic Calculator 題目鏈接:https://leetcode.com/problems... stack,就是感覺(jué)條件有點(diǎn)多 public class Solution { pub...

    _DangJin 評(píng)論0 收藏0
  • 227. Basic Calculator II

    摘要:但是乘除就會(huì)有問(wèn)題,要特殊處理。這題只有加減和括號(hào),優(yōu)先級(jí)就是括號(hào)里的先計(jì)算,所有我們把括號(hào)里的內(nèi)容當(dāng)做操作的基本單位。遇到遇到和,遇到遇到,彈出再遇到彈出,這里只是把對(duì)數(shù)字的操作變成了對(duì)的操作,去括號(hào)的邏輯一樣。 The expression string contains only non-negative integers, +, -, *, / operators and em...

    littlelightss 評(píng)論0 收藏0
  • [Leetcode] Basic Calculator 基本計(jì)算器

    摘要:難點(diǎn)在于多了括號(hào)后如何處理正負(fù)號(hào)。但是每多一個(gè)括號(hào),都要記錄下這個(gè)括號(hào)所屬的正負(fù)號(hào),而每當(dāng)一個(gè)括號(hào)結(jié)束,我們還要知道出來(lái)以后所在的括號(hào)所屬的正負(fù)號(hào)。 Basic Calculator I 最新更新請(qǐng)見(jiàn): https://yanjia.li/zh/2019/01/... Implement a basic calculator to evaluate a simple express...

    ky0ncheng 評(píng)論0 收藏0
  • Leetcode[224] Basic Calculator

    摘要:復(fù)雜度思路將字符串先轉(zhuǎn)換成后綴表達(dá)式,再將其出來(lái)。 Leetcode[224] Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ),...

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

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

0條評(píng)論

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