首页 > 其他分享 >LWC 50:679. 24 Game

LWC 50:679. 24 Game

时间:2023-07-10 16:32:43浏览次数:40  
标签:24 oo Set nums int cal Game ans LWC


LWC 50:679. 24 Game

传送门:679. 24 Game

Problem:

You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 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:

  • The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  • Every operation done is between two numbers. In particular, we cannot use - as a unary operator. For example, with [1, 1, 1, 1] as input, the expression -1 - 1 - 1 - 1 is not allowed.
  • You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

观察可以发现状态数非常少,所以完全可以采用暴力构造,暴力计算,暴力搜索。4个数字有24中情况,运算符有4种,3个位置有64种情况,所以只需要枚举出64 * 24中算数表达式的值即可。

我的代码:

char[] op = new char[] { '+', '-', '*', '/' };
    public boolean judgePoint24(int[] nums) {
        ops = new HashSet<>();
        permutation(nums, 0, new boolean[4], "");
        for (String oo : ops) {
            Set<Double> ans = cal(oo);
            for (double d : ans) {
                if (Math.abs(d - 24) <= 0.0000000001) return true;
            }
        }
        return false;
    }

    Set<Double> cal(String oo) {
        int cnt = 0;
        Set<Double> ans = new HashSet<>();
        for (int i = 0; i < oo.length(); ++i) {
            if (oo.charAt(i) == '+') {
                Set<Double> left = cal(oo.substring(0, i));
                Set<Double> right = cal(oo.substring(i + 1, oo.length()));
                cnt++;
                for (double a1 : left) {
                    for (double a2 : right) {
                        ans.add(a1 + a2);
                    }
                }
            }
            if (oo.charAt(i) == '-') {
                Set<Double> left = cal(oo.substring(0, i));
                Set<Double> right = cal(oo.substring(i + 1, oo.length()));
                cnt++;
                for (double a1 : left) {
                    for (double a2 : right) {
                        ans.add(a1 - a2);
                    }
                }
            }
            if (oo.charAt(i) == '*') {
                Set<Double> left = cal(oo.substring(0, i));
                Set<Double> right = cal(oo.substring(i + 1, oo.length()));
                cnt++;
                for (double a1 : left) {
                    for (double a2 : right) {
                        ans.add(a1 * a2);
                    }
                }
            }
            if (oo.charAt(i) == '/') {
                Set<Double> left = cal(oo.substring(0, i));
                Set<Double> right = cal(oo.substring(i + 1, oo.length()));
                cnt++;
                for (double a1 : left) {
                    for (double a2 : right) {
                        ans.add(a1 / a2);
                    }
                }
            }

        }
        if (cnt == 0) {
            ans.add(Double.parseDouble(oo));
            return ans;
        }
        return ans;
    }

    Set<String> ops;
    void permutation(int[] nums, int i, boolean[] vis, String ans) {
        if (i >= nums.length) {
            ops.add(ans.substring(0, ans.length() - 1));
            return;
        }
        for (int j = 0; j < nums.length; ++j) {
            if (!vis[j]) {
                vis[j] = true;
                for (int c = 0; c < 4; ++c) {
                    permutation(nums, i + 1, vis, ans + nums[j] + op[c]);
                }
                vis[j] = false;
            }
        }
    }

精简版本:

public boolean judgePoint24(int[] nums) {
        ops = new int[24][4];   
        permutation(nums, 0, new boolean[4], new int[4]);
        for (int i = 0; i < 24; ++i) {
            Set<Double> ans = cal(ops[i], 0, 3);
            for (double d : ans) {
                if (Math.abs(d - 24) <= 0.0000000001) return true;
            }
        }
        return false;
    }

    int[][] ops;
    int tot = 0;
    void permutation(int[] nums, int i, boolean[] vis, int[] tmp) {
        if (i == 4) {
            for (int j = 0; j < 4; ++j) {
                ops[tot][j] = tmp[j];
            }
            tot++;
            return;
        }
        for (int j = 0; j < nums.length; ++j) {
            if (!vis[j]) {
                tmp[i] = nums[j];
                vis[j] = true;
                permutation(nums, i + 1, vis, tmp);
                vis[j] = false;
            }
        }
    }

    Set<Double> cal(int[] nums, int start, int end){
        Set<Double> ans = new HashSet<>();
        if (start == end) {
            return Collections.singleton(new Double(nums[start]));
        }
        else {
            for (int i = start; i < end; ++i) {
                Set<Double> left = cal(nums, start, i);
                Set<Double> right = cal(nums, i + 1, end);
                for (double a1 : left) {
                    for (double a2 : right) {
                        ans.add(a1 + a2);
                        ans.add(a1 - a2);
                        ans.add(a1 * a2);
                        ans.add(a1 / a2);
                    }
                }
            }
            return ans;
        }
    }


标签:24,oo,Set,nums,int,cal,Game,ans,LWC
From: https://blog.51cto.com/u_16184402/6678323

相关文章

  • LWC 50:678. Valid Parenthesis String
    LWC50:678.ValidParenthesisString传送门:678.ValidParenthesisStringProblem:Givenastringcontainingonlythreetypesofcharacters:‘(‘,‘)’and‘*’,writeafunctiontocheckwhetherthisstringisvalid.Wedefinethevalidityofastringbythese......
  • LWC 50:677. Map Sum Pairs
    LWC50:677.MapSumPairs传送门:677.MapSumPairsProblem:ImplementaMapSumclasswithinsert,andsummethods.Forthemethodinsert,you’llbegivenapairof(string,integer).Thestringrepresentsthekeyandtheintegerrepresentsthevalue.Ifthekey......
  • LWC 54:697. Degree of an Array
    LWC54:697.DegreeofanArray传送门:697.DegreeofanArrayProblem:Givenanon-emptyarrayofnon-negativeintegersnums,thedegreeofthisarrayisdefinedasthemaximumfrequencyofanyoneofitselements.Yourtaskistofindthesmallestpossibleleng......
  • 2024备考408Week17
    一、本周总结:使用时间:总计25h,数学8h,专业课7h,英语5h,政治5h。二、存在问题:1.数学、专业课(DS+OS+CO+CN)做题训练不够,思考不够深入,计算不够熟练和准确,后期一定要开始加强了;2.碎片化时间和整块时间没有合理安排,碎片化时间应该安排英语/政治,整块时间应该安排数学/专业课。3.每周40h目标,1......
  • AT2402 题解
    题意简述给你\(n\)杯水,第\(i\)杯的水温为\(t_i\),容量为\(v_i\),依次倒入容量为\(V\)的大盆。注意每次倒入水后盆内水的总体积必须恒定为\(V\),且每杯水必须全部倒入,因此为防止倒进水时溢出,在倒水之前可以从盆里往外倒出一些水。求每次倒进水后盆里水温度的最大值(每次计算......
  • 抖音直播间视频怎么下载?如何24小时监控主播开播状态? 开播自动下载?
    大家好,我是安少大家应该都是通过搜索而来,安少给你带来最终的答案。随着直播行业的迅速发展,越来越多的加入了直播行业。玩法也越来越多样化。现在我们就需要自动帮直播的视频画面下载下来下载的视频可以用来无人直播,也可以做直播切片。那么具体应该如何使用呢?首先打开我们的工具然后......
  • 四个数 运算之后 结果是 24
    packageog.com;importjava.util.ArrayList;publicclassTest24{ArrayListal=newArrayList();publicstaticvoidmain(String[]args){int[]in={1,2,3,4};String[]fl={"+","-","*","/"};newTest24().s......
  • 24.solidwork零件导入cad图纸详解
    1.从其他软件导出或者绘制2维图纸,保存cad图纸2.用solidwrk打开保存的CAD图纸,选择2d草图,选择mm,导入后打钩3.单击左键crrl+c复制4.CTRL+V复制到要绘制的零件图中5.右键单击导图,选择编辑草图平面,选择要放置的草图平面位置后草图与平面共面6.进入编辑草图,点击移动实体命......
  • 大模型复现实践记录-在linux环境4090GPU(24G)
    chatglm-6btiger-7b......
  • BZOJ 4247: 挂饰 背包dp
    4247:挂饰TimeLimit: 10Sec  MemoryLimit: 256MBSubmit: 999  Solved: 387[Submit][Status][Discuss]DescriptionJOI君有N个装在手机上的挂饰,编号为1...N。JOI君可以将其中的一些装在手机上。JOI君的挂饰有一些与众不同——其中的一些挂饰附有可以挂其他挂件......