• 示例 1:

• 示例 2:

• 注意：

# 二、解题思路

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
b = nums
c = nums
d = nums

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)



05-11 12-23 2623
12-29
08-22 458
02-19 572
07-12 78
06-10 2987
02-01 931
02-28 230
04-10 882
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 点击重新获取   扫码支付 1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。 余额充值