1106. 解析布尔表达式

59 阅读1分钟

1106. 解析布尔表达式

hard 码力 暴力 思路 写法
的思路没有把握好
后缀表达式的理解不透彻

第一次手写

class Solution {
public:
    int change(char c) {
        return c == 't' ? 1 : 0;
    }

    char Not(string s) {
        return s == "t" ? 'f' : 't';
    }

    char And(string s) {
        int res = change(s[0]);
        for (int i = 1, n = s.length(); i < n; ++i) res &= change(s[i]);
        return res == 1 ? 't' : 'f';
    }

    char Or(string s) {
        int res = change(s[0]);
        for (int i = 1, n = s.length(); i < n; ++i) res |= change(s[i]);
        return res == 1 ? 't' : 'f';
    }
    
    bool parseBoolExpr(string s) {
        stack<char> num, op;
        char res;
        for (int i = 0, n = s.length(); i < n; ++i) {
            if (s[i] == 't') {
                num.push('t');
            } else if (s[i] == 'f') {
                num.push('f');
            } else if (s[i] == '(') {
                num.push('(');
            } else if (s[i] == ')') {
                string s = "";
                while (num.top() != '(') {
                    if (num.top() == 't' || num.top() == 'f') s += num.top();
                    num.pop();
                } 
                num.pop();
                char opr = op.top(); op.pop();
                if (opr == '!') num.push(Not(s));
                else if (opr == '&') num.push(And(s));
                else num.push(Or(s));
            } else if (s[i] == '!') {
                op.push('!');
            } else if (s[i] == '&') {
                op.push('&');
            } else if (s[i] == '|') {
                op.push('|');
            }
        }
        return num.top() == 'f' ? false : true;
    }
};

简洁 - C++

class Solution {
public:
    bool parseBoolExpr(string s) {
        stack<char> st;
        for (int i = 0, n = s.length(); i < n; ++i) {
            if (s[i] != '(' && s[i] != ')' && s[i] != ',') st.push(s[i]);
            else if (s[i] == ')') {
                int tt = 0, ff = 0;
                while (st.top() == 't' || st.top() == 'f') {
                    tt += st.top() == 't';
                    ff += st.top() == 'f';
                    st.pop();
                }
                char op = st.top(); st.pop();
                char tmp;
                if (op == '!') tmp = tt ? 'f' : 't';
                else if (op == '&') tmp = ff ? 'f' : 't';
                else tmp = tt ? 't' : 'f';
                st.push(tmp);
            }
        }
        return st.top() == 't' ? true : false;
    }
};

简洁 - Python

class Solution:
    def parseBoolExpr(self, s: str) -> bool:
        st = []
        for c in s:
            if c in 'tf!&|':
                st.append(c)
            elif c == ')':
                tt = ff = 0
                while st[-1] in 'tf':
                    tt += st[-1] == 't'
                    ff += st[-1] == 'f'
                    st.pop()
                match st.pop():
                    case '!':
                        c = 't' if ff else 'f'
                    case '&':
                        c = 'f' if ff else 't'
                    case '|':
                        c = 't' if tt else 'f'
                st.append(c)
        return st.pop() == 't'