679. 24 Game

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:

  1. The division operator / represents real division, not integer division. For example, 4 / (1 - 2/3) = 12.
  2. 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.
  3. You cannot concatenate numbers together. For example, if the input is [1, 2, 1, 2], we cannot write this as 12 + 12.

http://wiki.jikexueyuan.com/project/explore-python/Standard-Modules/itertools.html

直接暴力枚举,先确定数字排放顺序,然后决定计算顺序,然后决定2个数之间的计算方式 

from itertools import permutations

class Solution(object):
    def judgePoint24(self, nums):
        def f(s1, s2):
            ret = []
            for a in s1:
                for b in s2:
                    ret.append('(' + a + '+' + b + ')')
                    ret.append('(' + a + '-' + b + ')')
                    ret.append('(' + b + '-' + a + ')')
                    ret.append('(' + b + '/' + a + ')')
                    ret.append('(' + b + '/' + a + ')')
                    ret.append('(' + b + '*' + a + ')')
            return ret
        
        strs = [str(float(num)) for num in nums]
        for c in permutations(strs):
            # 可以有2中结合方式
            eq1 = f(f(f([c[0]], [c[1]]), [c[2]]), [c[3]])
            eq2 = f(f([c[0]], [c[1]]), f([c[2]], ([c[3]])))
            for eq in eq1+eq2:
                try:
                    if 23.99 < eval(eq) < 24.01:
                        return True
                except:
                    pass
        return False


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:Rhino对于指定包名或类名时是如何处理错误的。如果java.Myclass是可访问的,Rhino会试图加载名为java.MyClass的类,如果加载失败,它会假设java.MyClass是一个包名,不会报错。\[1\]引用\[2\]:要注意Java会暗中导入java.lang.*,但是Rhino不会。因为JavaScript的顶层对象Boolean、Math、Number、Object和String和java.lang包中同名的类并不相同。因为这种冲突,建议不要用importPackage来导入java.lang包。\[2\] 根据提供的错误信息"Exception in thread "main" java.lang.NullPointerException at game8.GameMap.createMap(GameMap.java:54) at game8.GameController.runGame(GameController.java:29) at game8.GameController.<init>(GameController.java:21) at game8.GameTest.main(GameTest.java:5)",这是一个空指针异常(NullPointerException)。空指针异常通常发生在试图访问一个空对象的属性或调用一个空对象的方法时。根据错误信息,异常发生在GameMap类的第54行,可能是因为在该行代码中访问了一个空对象的属性或方法。为了解决这个问题,你需要检查GameMap类的第54行代码,并确保在访问对象之前对其进行了正确的初始化。 #### 引用[.reference_title] - *1* *2* *3* [【Auto.JS】Autojs官方提取文档使用说明函数 (2)](https://blog.csdn.net/a6892255/article/details/107256535)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值