leetcode_679.24 点游戏 python3
【摘要】 目录
一、题目内容
二、解题思路
三、 代码
一、题目内容
游戏规则:你有 4 张写有 1 到 9 数字的牌。你需要判断是否能通过 *,/,+,-,(,) 的运算得到 24。
示例 1:
输入: [4, 1, 8, 7] 输出: True 解释: (8-4) * (7-1) = 24
示例 2:
输入:...
目录
一、题目内容
游戏规则:你有 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)
-
文章来源: nickhuang1996.blog.csdn.net,作者:悲恋花丶无心之人,版权归原作者所有,如需转载,请联系作者。
原文链接:nickhuang1996.blog.csdn.net/article/details/108175158
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)