leetcode679:24Game
考虑1,5,5,5这种情况,有:5*(5-1/5)=24
所以除法必须自定义运算才行。
class Num:
def __init__(self,up,down=1):
self.up=up
self.down=down
def gcd(self,x,y):
return x if y==0 else self.gcd(y,x%y)
def simple(self):
if self.up==0:
return
d=self.gcd(self.up,self.down)
self.up//=d
self.down//=d
def mul(self,x):
res=Num(self.up*x.up,self.down*x.down)
res.simple()
return res
def div(self,x):
res=Num(self.up*x.down,self.down*x.up)
res.simple()
return res
def add(self,x):
res=Num(self.up*x.down+self.down*x.up,self.down*x.down)
res.simple()
return res
def sub(self,x):
res=Num(self.up*x.down-self.down*x.up,self.down*x.down)
res.simple()
return res
def __str__(self):
if self.down==1:return str(self.up)
return "{}/{}".format(self.up,self.down)
class Solution(object):
def judgePoint24(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def op(x,y,o):
if o==0:
return x.mul(y)
elif o==1:
return x.div(y)
elif o==2:
return x.add(y)
elif o==3:
return x.sub(y)
elif o==4:
return y.sub(x)
else:
return y.div(x)
def newar(a,i,j,z):
ans=[z]
for k in range(len(a)):
if k!=i and k!=j:
ans.append(a[k])
return ans
def tos(x):
return ','.join([str(i) for i in x])
def go(nums):
if len(nums)==1:
if nums[0].up==24 and nums[0].down==1:
return True
else:
return False
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(6):
z=op(nums[i],nums[j],k)
res=go(newar(nums,i,j,z))
if res:
return True
return False
nums=[Num(i)for i in nums]
return go(nums)
leetcode679:24Game的更多相关文章
- [Swift]LeetCode679. 24点游戏 | 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- LeetCode679. 24 Game
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- [LeetCode] 24 Game 二十四点游戏
You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...
- PAT 2-08. 用扑克牌计算24点(25):
题目链接:http://www.patest.cn/contests/ds/2-08 解题思路:思路参考24点游戏技巧http://www.24game.com.cn/articles/points2 ...
- 每日一题 LeetCode 679. 24点游戏 【递归】【全排列】
题目链接 https://leetcode-cn.com/problems/24-game/ 题目说明 题解 主要方法:递归 + 全排列 解释说明: 将 4 个数进行组合形成算式,发现除了 (a❈b) ...
随机推荐
- 服务信息块协议 SMB(Server Message Block protocol)
SMB(Server Message Block)是协议名,它能被用于Web连接和客户端与服务器之间的信息沟通. SMB协议 SMB最初是IBM的贝瑞·费根鲍姆(Barry Feigenbaum)研制 ...
- 混沌数学之CircuitChaotic(二维离散电路混沌系统)
相关软件参见:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/link?url=yg_gE7LUXCg2mXRp-ZZdfRXXIkcNj8YOhvN7 ...
- Informatica 常用组件Lookup之五 转换属性
查找转换的属性标识数据库源.PowerCenter 如何处理转换,以及它如何处理高速缓存和多项匹配. 创建映射时,为每个查找转换指定属性.创建会话时,您可在会话属性中覆盖某些属性,如每个转换的索引和数 ...
- 让IE6/IE7/IE8支持HTML5标签的js代码
让IE(ie6/ie7/ie8)支持HTML5元素,我们需要在HTML头部添加以下JavaScript,这是一个简单的document.createElement声明,利用条件注释针对IE来调用这个j ...
- 校验IPv4和IPv6地址和URL地址
1.校验IPV4地址: function validateIp(obj) { var ip=$(obj).val(); var re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;// ...
- WordPress 后台上传自定义网站Logo
需求: 众所周知一般网站的logo都是固定的所以我在做网站时也是使用的静态logo文件,但最近用wp给一个客户做的网站时,因为网站现在的logo可能会需要重新设计,所以客户提出了需要在后台可以自己修改 ...
- iOS_2_button控制物体形变
终于效果图: BeyondViewController.h // // BeyondViewController.h // 02_button控制物体形变 // // Created by beyon ...
- ElasticSearch关闭重启命令
很多人学习elasticSearch都是自学,想百度一下如何重启es也是没有答案,我硬着头皮,算是琢磨出来了,借此写博,希望能帮助您. 1.如何关闭ES,elasticsearch关闭办法 1.使用h ...
- Drupal 通过API动态的加入样式文件
前面几篇文章中讲到关于样式的载入方式.已经了解到能够通过 theme.info 载入样式文件,但都须要更新缓存才干够使用.因些这样子没有办法动态的载入一些样式文件,在DP中提供了两个API操作样式文件 ...
- 【Nodejs】使用put方式向后端查询数据并在页面显示
前端代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Ty ...