leetcode_679.24 点游戏 python3

 目录

一、题目内容

二、解题思路

三、 代码


一、题目内容

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

  • 示例 1:

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

  • 示例 2:

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

  • 注意:

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

二、解题思路

1.递归处理,4->3->2

2.注意分母不能为0否则ZeroDivisionError

3.python3代码,如果换成python则测试用例[8,1,6,6]不通过(应为True,python为False,python3为True)

三、 代码

class Solution(object):
    def judgePoint24(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        a = nums[0]
        b = nums[1]
        c = nums[2]
        d = nums[3]

        def iter_judge_4(a, b, c, d):
            return \
                iter_judge_3(a + b, c, d) or \
                iter_judge_3(a - b, c, d) or \
                iter_judge_3(a * b, c, d) or \
                (iter_judge_3(a / b, c, d) if b != 0 else False) or \
                iter_judge_3(b - a, c, d) or \
                (iter_judge_3(b / a, c, d) if a != 0 else False) or \
                \
                iter_judge_3(b + c, a, d) or \
                iter_judge_3(b - c, a, d) or \
                iter_judge_3(b * c, a, d) or \
                (iter_judge_3(b / c, a, d) if c != 0 else False) or \
                iter_judge_3(c - b, a, d) or \
                (iter_judge_3(c / b, a, d) if b != 0 else False) or \
                \
                iter_judge_3(c + d, a, b) or \
                iter_judge_3(c - d, a, b) or \
                iter_judge_3(c * d, a, b) or \
                (iter_judge_3(c / d, a, b) if d != 0 else False) or \
                iter_judge_3(d - c, a, b) or \
                (iter_judge_3(d / c, a, b) if c != 0 else False) or \
                \
                iter_judge_3(a + c, b, d) or \
                iter_judge_3(a - c, b, d) or \
                iter_judge_3(a * c, b, d) or \
                (iter_judge_3(a / c, b, d) if c != 0 else False) or \
                iter_judge_3(c - a, b, d) or \
                (iter_judge_3(c / a, b, d) if a != 0 else False) or \
                \
                iter_judge_3(a + d, b, c) or \
                iter_judge_3(a - d, b, c) or \
                iter_judge_3(a * d, b, c) or \
                (iter_judge_3(a / d, b, c) if d != 0 else False) or \
                iter_judge_3(d - a, b, c) or \
                (iter_judge_3(d / a, b, c) if a != 0 else False) or \
                \
                iter_judge_3(b + d, a, c) or \
                iter_judge_3(b - d, a, c) or \
                iter_judge_3(b * d, a, c) or \
                (iter_judge_3(b / d, a, c) if d != 0 else False) or \
                iter_judge_3(d - b, a, c) or \
                (iter_judge_3(d / b, a, c) if b != 0 else False)

        def iter_judge_3(a, b, c):
            return \
                iter_judge_2(a + b, c) or \
                iter_judge_2(a * b, c) or \
                iter_judge_2(a - b, c) or \
                (iter_judge_2(a / b, c) if b != 0 else False) or \
                iter_judge_2(b - a, c) or \
                (iter_judge_2(b / a, c) if a != 0 else False) or \
                \
                iter_judge_2(b + c, a) or \
                iter_judge_2(b * c, a) or \
                iter_judge_2(b - c, a) or \
                (iter_judge_2(b / c, a) if c != 0 else False) or \
                iter_judge_2(c - b, a) or \
                (iter_judge_2(c / b, a) if b != 0 else False) or \
                \
                iter_judge_2(c + a, b) or \
                iter_judge_2(c * a, b) or \
                iter_judge_2(c - a, b) or \
                (iter_judge_2(c / a, b) if a != 0 else False) or \
                iter_judge_2(a - c, b) or \
                (iter_judge_2(a / c, b) if c != 0 else False)

        def iter_judge_2(a, b):
            return \
                abs(a + b - 24) < 0.001 or \
                abs(a - b - 24) < 0.001 or \
                abs(a * b - 24) < 0.001 or \
                (abs(a / b - 24) < 0.001 if b != 0 else False) or \
                abs(b - a - 24) < 0.001 or \
                (abs(b / a - 24) < 0.001 if a != 0 else False)

        ans = iter_judge_4(a, b, c, d)
        return ans


if __name__ == '__main__':
    nums = [8,1,6,6]
    s = Solution()
    ans = s.judgePoint24(nums)
    print(ans)

 

悲恋花丶无心之人 CSDN认证博客专家 深度学习 神经网络 Pytorch
计算机视觉在读研究生,熟悉Pytorch,MXNet,TensorFlow,Keras等深度学习框架,主要涉及的领域有目标检测,语义分割,超分辨率重建,行人重识别等。
个人GitHub网址为:https://github.com/nickhuang1996
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付 29.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值