LeetCode 224. 基本計算機

來源:互聯網
上載者:User

標籤:簡潔   new   說明   style   first   ++   color   表達   blog   

實現一個基本的計算機來計算一個簡單的字串運算式的值。

字串運算式可以包含左括弧 ( ,右括弧 ),加號 + ,減號 -,非負整數和空格  

樣本 1:

輸入: "1 + 1"輸出: 2

樣本 2:

輸入: " 2-1 + 2 "輸出: 3

樣本 3:

輸入: "(1+(4+5+2)-3)+(6+8)"輸出: 23

說明:

  • 你可以假設所給定的運算式都是有效。
  • 請不要使用內建的庫函數 eval

一開始寫的代碼如下,太冗餘了:

class Solution {public:    int calculate(string s) {        stack<int> s1;        stack<char> s2;        int curRes = 0;        for (int i = 0; i < s.size(); ) {            if (s[i] == ‘ ‘) {                i++;            }            else if (s[i] == ‘+‘ || s[i] == ‘-‘ || s[i] == ‘(‘) {                s2.push(s[i++]);            }            else if (isdigit(s[i])) {                int tmp = 0, j = i;                while (j < s.size() && isdigit(s[j])) {                    tmp = 10 * tmp + s[j++] - ‘0‘;                }                s1.push(tmp);                i = j;                if (!s2.empty() && s2.top() != ‘(‘) {                    char ctmp = s2.top();   s2.pop();                    int second, first;                    second = s1.top();  s1.pop();                    first = s1.top();   s1.pop();                    curRes = ctmp == ‘+‘ ? first + second : first - second;                    s1.push(curRes);                }            }            else {                s2.pop();//(                char op = ‘?‘;                int first = 0, second;                if (!s2.empty()) {                    op = s2.top();    s2.pop();                    second = s1.top();    s1.pop();                    if (!s1.empty()) {                        first = s1.top();    s1.pop();                    }                    if (op == ‘+‘) {                        s1.push(first + second);                    }                    else if (op == ‘-‘) {                        s1.push(first - second);                    }                    else {                        s1.push(second);                    }                }                i++;            }        }        return s1.empty()? 0: s1.top();    }};

後來在網上發現了更簡潔的寫法,參考https://www.cnblogs.com/newnoobbird/p/9627182.html。非常簡潔,之利用了

class Solution {public:    int calculate(string s) {        int res=0,sign=1,n=s.size();        stack<int> st;        for(int i=0;i<n;i++){            if(s[i]>=‘0‘){                int num=0;                while(i<n&&s[i]>=‘0‘){                    num=num*10+s[i++]-‘0‘;                }                res+=sign*num;                i--;            }else if(s[i]==‘+‘){                sign=1;            }else if(s[i]==‘-‘){                sign=-1;            }else if(s[i]==‘(‘){                st.push(res);                st.push(sign);                res=0; sign=1;            }else if(s[i]==‘)‘){                res*=st.top(); st.pop();                res+=st.top(); st.pop();            }        }        return res;    }};

 

LeetCode 224. 基本計算機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.