【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 ...
随机推荐
- C# 枚举的声名和使用
namespace xxxxxx { public enum EnumTextHAlign { Left = , Center = , Right = } } using xxxxxx;
- 【leetcode】1012. Complement of Base 10 Integer
题目如下: Every non-negative integer N has a binary representation. For example, 5 can be represented a ...
- leetcode-166周赛-5282-转化为全0矩阵的最小反转次数
题目描述: 方法一:暴力BFS class Solution: def minFlips(self, mat) -> int: R, C = len(mat), len(mat[0]) def ...
- dotnet 跨平台编译发布
dotnet publish 命令,bash脚本如下(Windows安装git即可建议sh关联) publish.sh #!/usr/bin/env bash # one line command: ...
- 网络体系应用层之万维网、http协议
1.万维网概述 万维网以客户--服务器方式工作,万维网客户程序就是各式各样的浏览器,万维网文档所驻留的主机则运行服务器程序, 因此这个主机也称为万维网服务器.客户程序向服务器程序发出请求,服务器程序向 ...
- [CSP-S模拟测试]:序列(主席树)
题目描述 小$A$把自己之前得到的序列展示给了小$B$,不过这一次,他并不要求小$B$模仿他之前的行为.他给了小$B$一些询问,每个询问都是$l\ r\ x$的形式,要求小$B$数出在序列的第$l$个 ...
- vue-cli2.X之simple项目搭建过程
1.vue init webpack-simple vuedemo02 2.按提示操作 3. 项目目录: ps:可能遇到的问题
- 后端技术杂谈3:Lucene基础原理与实践
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- 使用mysql应该注意的细节
一.表及字段的命名规范 1.可读性原则 使用大写和小写来格式化的库对象名字以获得良好的可读性. 例如:使用CustAdress而不是custaddress来提高可读性.(这里注意有些DBMS系统对表名 ...
- 二次封装dojo slider
上次的二次封装timeslider,挺有意思,又来封装一个dojo的,样式还是用arcgis的.实现更多功能,包括HorizontalSlider和VerticalSlider, 刻度的显示隐藏,标签 ...