Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

679. 24 Game #307

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

679. 24 Game

你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 */+-() 的运算得到 24。

Example 1

Input: [4, 1, 8, 7]
Output: True
Explanation: (8-4) * (7-1) = 24

Example 2

Input: [1, 2, 1, 2]
Output: False

Note

Tcdian commented 3 years ago

Solution

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var judgePoint24 = function(nums) {
    let result = false;
    const operators = ['+', '-', '*', '/'];
    backtracking(nums);
    return result;

    function backtracking(nums) {
        if (result) {
            return;
        }
        if (nums.length === 1) {
            result = Math.abs(nums[0] - 24) < 0.0000001;
            return;
        }
        for (let i = 0; i < nums.length; i++) {
            for (let j = 0; j < nums.length; j++) {
                if (i === j) {
                    continue;
                }
                for (let s = 0; s < operators.length; s++) {
                    if (i < j) {
                        backtracking([
                            ...nums.slice(0, i),
                            ...nums.slice(i + 1, j),
                            ...nums.slice(j + 1),
                            calc(nums[i], nums[j], operators[s])
                        ]);
                    } else {
                        backtracking([
                            ...nums.slice(0, j),
                            ...nums.slice(j + 1, i),
                            ...nums.slice(i + 1),
                            calc(nums[i], nums[j], operators[s])
                        ]);
                    }
                }
            }
        }
    }
    function calc(a, b, operator) {
        switch(operator) {
            case '+':
                return a + b;
            case '-':
                return a - b;
            case '*':
                return a * b;
            case '/':
                return a / b;
        }
    }
};
function judgePoint24(nums: number[]): boolean {
    let result = false;
    const operators: ['+', '-', '*', '/'] = ['+', '-', '*', '/'];
    backtracking(nums);
    return result;

    function backtracking(nums: number[]) {
        if (result) {
            return;
        }
        if (nums.length === 1) {
            result = Math.abs(nums[0] - 24) < 0.0000001;
            return;
        }
        for (let i = 0; i < nums.length; i++) {
            for (let j = 0; j < nums.length; j++) {
                if (i === j) {
                    continue;
                }
                for (let s = 0; s < operators.length; s++) {
                    if (i < j) {
                        backtracking([
                            ...nums.slice(0, i),
                            ...nums.slice(i + 1, j),
                            ...nums.slice(j + 1),
                            calc(nums[i], nums[j], operators[s])
                        ]);
                    } else {
                        backtracking([
                            ...nums.slice(0, j),
                            ...nums.slice(j + 1, i),
                            ...nums.slice(i + 1),
                            calc(nums[i], nums[j], operators[s])
                        ]);
                    }
                }
            }
        }
    }
    function calc(a: number, b: number, operator: '+' | '-' | '*' | '/') {
        switch(operator) {
            case '+':
                return a + b;
            case '-':
                return a - b;
            case '*':
                return a * b;
            case '/':
                return a / b;
        }
    }
};