【leetcode】679. 24 Game
题目如下:

解题思路:24点是非常经典的游戏了,因为本题数据量小,可以使用穷举法,把所有的可能结果都算出来。假设nums = [a,b,c,d],记f(n)表示用nums中n个数字进行运算可以得到的结果集,那么f(4)又可以记为f(4) = U(U(f(1),f(3), U(f(2),f(2))) (U表示合集),而f(3)又可以记为f(3) = U(f(1),f(2))。可知f(1)的结果集就是参数本身,f(2)的结果集是两个参数a,b进行四则运算的结果,最后判断24是否在结果集中即可。有一点要注意的是,除法运算会有浮点数产生,所以与24比较的时候要有一个浮动的区间。
代码如下:
class Solution(object):
def union(self,m,n):
r = []
for i in range(len(m)):
for j in range(len(n)):
r.append(m[i] + n[j])
r.append(m[i] - n[j])
r.append(m[i] * n[j])
if n[j] != 0:
r.append(float(m[i]) / float(n[j]))
if m[i] != 0:
r.append(float(n[j]) / float(m[i]))
return r def calc(self,l):
if len(l) == 1:
return l
elif len(l) == 2:
r = [l[0] + l[1],l[0] - l[1],l[0] * l[1],l[1] - l[0]]
if l[1] != 0:
r.append(float(l[0])/float(l[1]))
if l[0] != 0:
r.append(float(l[1])/float(l[0]))
return r
elif len(l) == 3:
return self.union(self.calc([l[0]]) , self.calc([l[1],l[2]])) + \
self.union(self.calc([l[1]]) , self.calc([l[0],l[2]])) + \
self.union(self.calc([l[2]]) , self.calc([l[0],l[1]])) def judgePoint24(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
r = [([nums[0]], [nums[1], nums[2], nums[3]]), ([nums[1]], [nums[0], nums[2], nums[3]]),
([nums[2]], [nums[0], nums[1], nums[3]]),
([nums[3]], [nums[0], nums[1], nums[2]]), ([nums[0], nums[1]], [nums[2], nums[3]]),
([nums[0], nums[2]], [nums[1], nums[3]]), ([nums[0], nums[3]], [nums[1], nums[2]])] for m,n in r:
m = self.calc(m)
n = self.calc(n)
result = self.union(m,n)
for res in result:
if abs(float(24) - float(res)) < 0.00000001:
return True
return False
【leetcode】679. 24 Game的更多相关文章
- 【LeetCode】911. Online Election 解题报告(Python)
[LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...
- 【LeetCode】886. Possible Bipartition 解题报告(Python)
[LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】870. Advantage Shuffle 解题报告(Python)
[LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】729. My Calendar I 解题报告
[LeetCode]729. My Calendar I 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/my-calendar- ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
随机推荐
- Linux的cat命令详解
The cat command reads one or more files and copies them to standard output 也就是说,cat命令读取文件,并显示在 stand ...
- Appium环境搭建(Appium库的安装)
Appium环境搭建 谷歌驱动和对照:注意:64位向下兼容,直接下载32位的就可以啦,亲测可用. https://blog.csdn.net/allthewayforward/article/deta ...
- 二、制作BOM表格--物料表格--Bill of Materials
二.制作BOM表格--物料表格--Bill of Materials 公司会根据这个表格进行相关元器件的采购--以及后期的贴片上彩 操作: .dsn--Tools--Bill of Materials ...
- 31 July
P1005 矩阵取数游戏 高精度不能更坑-- #include <cstdio> #include <cstring> struct INT { long long h, l; ...
- vue绑定属性、绑定class及绑定style
1.绑定属性 v-bind 或者 : 例如:<img :src="pic_src" /> <template> <div id="app& ...
- <HTTP权威指南>记录 ---- 网络爬虫
网络爬虫 网络爬虫(web crawler)能够在无需人类干预的情况下自动进行一系列Web事务处理的软件程序.很多爬虫会从一个Web站点逛到另一个Web站点,获取内容,跟踪超链,并对它们找到的数据进行 ...
- python中的list和generator
# -*- coding:utf-8 -*- # author : Keekuun # 列表生成式,list l1 = [i for i in range(10)] # 或者 l2 = list(ra ...
- 杂项:JFB-权限设置
ylbtech-杂项:JFB-权限设置 1. 家政经理返回顶部 1. if (UserContext.GetTeamId() == (int)UserType.Manager) { condition ...
- 113、TensorFlow变量集合
#一个tensorflow程序断开的部分可能要创建变量 # 如果有一种方法来访问所有的变量是非常有用的 #因为这个原因TensorFlow提供了集合,是一些张量的集合 #或者是其他的对象,就像tf.V ...
- springboot 应用程序的文件检索描述
SpringApplication从application.properties以下位置的文件加载属性并将它们添加到Spring Environment: 一个/config当前目录下的子目录. 当前 ...