Untitled

 avatar
unknown
plain_text
10 months ago
1.5 kB
6
Indexable
All Submissions

Accepted
jefflig
jefflig
submitted at Jun 07, 2020 15:06

Editorial
Runtime
2
ms
Beats
83.15%
Analyze Complexity
Memory
36.85
MB
Beats
100.00%
Analyze Complexity
Code
Java
class Solution {
    
    public boolean equals24(double d) {
        return d < 24.001 && d > 23.999;
    }
    
    public double applyOp(int op, double x, double y) {
        if (op == 0) return x + y;
        if (op == 1) return x - y;
        if (op == 2) return x * y;
        if (op == 3) return x / y;
        return -1;
    }
    
    public boolean tryAllOps(int a, int b, int c, int d) {
        for (int op1 = 0; op1 < 4; op1++) {
            for (int op2 = 0; op2 < 4; op2++) {
                for (int op3 = 0; op3 < 4; op3++) {
                    if (equals24(applyOp(op3, d, applyOp(op2, c, applyOp(op1, b, a))))
                       || equals24(applyOp(op3, applyOp(op2, d, c), applyOp(op1, b, a)))) return true;
                }
            }
        }
        return false;
    }
    
    public boolean judgePoint24(int[] nums) {
        for (int a = 0; a < 4; a++) {
            for (int b = 0; b < 4; b++) {
                for (int c = 0; c < 4; c++) {
                    for (int d = 0; d < 4; d++) {
                        if (a == b || a == c || a == d || b == c || b == d || c == d) continue;
                        if (tryAllOps(nums[a], nums[b], nums[c], nums[d])) return true;
                    }
                }
            }
        }
        return false;
        
    }
}
Editor is loading...
Leave a Comment