java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java C++解析布尔表达式

Java C++刷题leetcode1106解析布尔表达式

作者:AnjaVon

这篇文章主要为大家介绍了Java C++刷题leetcode1106解析布尔表达式示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

题目

题目要求

思路:栈【计算器】

Java

class Solution {
    public boolean parseBoolExpr(String expression) {
        Deque<Character> tfs = new ArrayDeque<>(), opts = new ArrayDeque<>();
        for (char c : expression.toCharArray()) {
            if (c == ',')
                continue;
            else if (c == 't' || c == 'f' || c == '(')
                tfs.addLast(c);
            else if (c == '|' || c == '&' || c == '!')
                opts.addLast(c);
            else if (c == ')') {
                char op = opts.pollLast(), cur = ' ';
                while (!tfs.isEmpty() && tfs.peekLast() != '(') {
                    char top = tfs.pollLast();
                    cur = cur == ' ' ? top : calBool(top, cur, op);
                }
                if (op == '!')
                    cur = cur == 't' ? 'f' : 't';
                tfs.pollLast();
                tfs.addLast(cur);
            }
        }
        return tfs.peekLast() == 't';
    }
    char calBool(char cx, char cy, char op) {
        boolean bx = cx == 't', by = cy == 't';
        boolean res = op == '|' ? bx | by : bx & by;
        return res ? 't' : 'f';
    }
}

C++

class Solution {
public:
    bool parseBoolExpr(string expression) {
        stack<char> tfs, opts;
        for (auto c : expression) {
            if (c == ',')
                continue;
            else if (c == 't' || c == 'f' || c == '(')
                tfs.push(c);
            else if (c == '|' || c == '&' || c == '!')
                opts.push(c);
            else if (c == ')') {
                char op = opts.top(), cur = ' ';
                opts.pop();
                while (!tfs.empty() && tfs.top() != '(') {
                    char top = tfs.top();
                    tfs.pop();
                    cur = cur == ' ' ? top : calBool(top, cur, op);
                }
                if (op == '!')
                    cur = cur == 't' ? 'f' : 't';
                tfs.pop();
                tfs.push(cur);
            }
        }
        return tfs.top() == 't';
    }
    char calBool(char cx, char cy, char op) {
        bool bx = cx == 't', by = cy == 't';
        bool res = op == '|' ? bx | by : bx & by;
        return res ? 't' : 'f';
    }
};

Rust

impl Solution {
    pub fn parse_bool_expr(expression: String) -> bool {
        let (mut tfs, mut opts) = (vec![], vec![]);
        for c in expression.chars() {
            if c == 't' || c == 'f' || c == '(' {
                tfs.push(c);
            }
            else if c == '|' || c == '&' || c == '!' {
                opts.push(c);
            }
            else if c == ')' {
                let op = opts.pop().unwrap();
                let mut cur = 'e';
                while !tfs.is_empty() && tfs[tfs.len() - 1] != '(' {
                    let top = tfs.pop().unwrap();
                    if cur == 'e' {
                        cur = top;
                    }
                    else { // fn calBool()
                        let (bx, by, mut tmp) = (top == 't', cur == 't', false);
                        if op == '|' {
                            tmp = bx | by;
                        }
                        else {
                            tmp = bx & by;
                        }
                        if tmp {
                            cur = 't';
                        }
                        else {
                            cur = 'f';
                        }
                    }
                }
                if op == '!' { // 非
                    if cur == 't' {
                        cur = 'f';
                    }
                    else {
                        cur = 't';
                    }
                }
                tfs.pop();
                tfs.push(cur);
            }
        }
        tfs.pop().unwrap() == 't'
    }
}

总结

以上就是Java C++刷题leetcode1106解析布尔表达式的详细内容,更多关于Java C++解析布尔表达式的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文