如果你想在校招中顺利拿到更好的offer,阿秀建议你多看看前人的经验 ,比如准备简历实习校招总结offer选择也欢迎来一起参加秋招打卡活动 等;如果你是计算机小白,学习/转行/校招路上感到迷茫或者需要帮助,可以点此联系阿秀;免费分享阿秀个人学习计算机以来的收集到的好资源,点此白嫖;如果你需要《阿秀的学习笔记》网站中求职相关知识点的PDF版本的话,可以点此下载

# 679. 24 点游戏

力扣原题链接(点我直达) (opens new window)

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

示例 1:

输入: [4, 1, 8, 7]
输出: True
解释: (8-4) * (7-1) = 24
1
2
3

示例 2:

输入: [1, 2, 1, 2]
输出: False
1
2

注意:

  1. 除法运算符 / 表示实数除法,而不是整数除法。例如 4 / (1 - 2/3) = 12 。
  2. 每个运算符对两个数进行运算。特别是我们不能用 - 作为一元运算符。例如,[1, 1, 1, 1] 作为输入时,表达式 -1 - 1 - 1 - 1 是不允许的。
  3. 你不能将数字连接在一起。例如,输入为 [1, 2, 1, 2] 时,不能写成 12 + 12 。

# 1、直接穷举

class Solution {
    public boolean judgePoint24(int[] nums) {
        double a = nums[0];
        double b = nums[1];
        double c = nums[2];
        double d = nums[3];
        return judge(a, b, c, d);
    }
    public boolean judge(double a, double b, double c, double d){
        return 
        // a b 
        judge(a + b, c, d) ||
        judge(a * b, c, d) ||
        judge(a - b, c, d) ||  
        judge(b - a, c, d) ||
        judge(a / b, c, d) ||
        judge(b / a, c, d) ||
        // b c 
        judge(c + b, a, d) ||
        judge(c * b, a, d) ||
        judge(c - b, a, d) ||  
        judge(b - c, a, d) ||
        judge(c / b, a, d) ||  
        judge(b / c, a, d) ||
        // c d 
        judge(c + d, a, b) ||
        judge(c * d, a, b) ||
        judge(c - d, a, b) ||  
        judge(d - c, a, b) ||
        judge(c / d, a, b) ||
        judge(d / c, a, b) ||
        // b d 
        judge(b + d, a, c) ||
        judge(b * d, a, c) ||
        judge(b - d, a, c) ||  
        judge(d - b, a, c) ||
        judge(b / d, a, c) ||
        judge(d / b, a, c) ||
        // a c 
        judge(a + c, b, d) ||
        judge(a * c, b, d) || 
        judge(a - c, b, d) ||  
        judge(c - a, b, d) ||
        judge(a / c, b, d) ||
        judge(c / a, b, d) ||
        // a d
        judge(a + d, b, c) ||
        judge(a * d, b, c) ||
        judge(a - d, b, c) ||  
        judge(d - a, b, c) ||
        judge(a / d, b, c) ||
        judge(d / a, b, c) ;
        //
    }
    public boolean judge(double a, double b, double c){// 24 , 3 , 8
        return 
        judge(a + b, c) ||
        judge(a * b, c) ||
        judge(a - b, c) ||  
        judge(b - a, c) || 
        judge(a / b, c) || 
        judge(b / a, c) ||
        //
        judge(c + b, a) ||
        judge(c * b, a) ||
        judge(c - b, a) ||  
        judge(b - c, a) ||
        judge(c / b, a) ||
        judge(b / c, a) ||
        //
        judge(c + a, b) ||
        judge(c * a, b) ||
        judge(c - a, b) ||  
        judge(a - c, b) ||
        judge(c / a, b) ||
        judge(a / c, b);
    }

    public boolean judge(double a, double b){
        return 
        Math.abs(a + b - 24) < 0.001 ||
        Math.abs(a - b - 24) < 0.001 || 
        Math.abs(b - a - 24) < 0.001 ||
        Math.abs(a * b - 24) < 0.001 ||
        Math.abs(a / b - 24) < 0.001 ||
        Math.abs(b / a - 24) < 0.001;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88