题目链接

考虑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的更多相关文章

  1. [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 ...

  2. LeetCode679. 24 Game

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  3. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  4. PAT 2-08. 用扑克牌计算24点(25):

    题目链接:http://www.patest.cn/contests/ds/2-08 解题思路:思路参考24点游戏技巧http://www.24game.com.cn/articles/points2 ...

  5. 每日一题 LeetCode 679. 24点游戏 【递归】【全排列】

    题目链接 https://leetcode-cn.com/problems/24-game/ 题目说明 题解 主要方法:递归 + 全排列 解释说明: 将 4 个数进行组合形成算式,发现除了 (a❈b) ...

随机推荐

  1. 约合¥1720 LG法国称G Watch将于6月开卖

    近来LG法国官方透露音讯称旗下首款智能手表G Watch将于本年6月份正式出售,预定报价为199欧元(约合¥1720). 这就意味着这款智能手表会在googleI/O大会完成之后就会开端出售,从goo ...

  2. E470 外放没声音问题解决

    到官网下载声卡驱动.和热键驱动,安装就ok了

  3. 如何处理wordpress首页不显示指定分类文章

    如何实现wordpress首页不显示指定分类文章,要实现这一步,首先必须找到需要屏蔽的该目录的id,那么如何查看wordpress的分类id呢?有两种方法: 通过wordpress后台查看分类的ID ...

  4. go语言基础之指针做函数参数用地址传递

    1.指针做函数参数 示例: package main //必须有个main包 import "fmt" func swap(p1, p2 *int) { *p1, *p2 = *p ...

  5. retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】

    项目地址:https://github.com/baiqiantao/retrofit2_okhttp3_RxJava_butterknife.git <uses-permission andr ...

  6. DataTrigger

    <ListView Name="lvStatus" MinHeight="120" Grid.Row="2"> <List ...

  7. Cognos两种建模工具对于复杂日期维度的处理比较(下)

    本文继Cognos两种建模工具对于复杂日期维度的处理比较(上)之后将介绍一下Cognos中建模工具Transform对复杂日期维度的处理. 二:Transform建模对于复杂日期维度的处理 为了书写效 ...

  8. [android错误] Installation error: INSTALL_FAILED_VERSION_DOWNGRA

    错误表现: [2014-06-27 18:19:51 - XXX] Installing XXXX.apk... [2014-06-27 18:20:00 - XXX] Installation er ...

  9. Discuz常见小问题-如何快速安装和配置

    下载PHPNOW 可以解压到本地的某个目录,最好不要有中文路径,然后查看Readme进行安装,双击Setup.cmd 安装结束之后,会要求输入一个初始化的密码,不要忘记,会自动弹出一个测试页面,可以测 ...

  10. 自定义cas客户端核心过滤器AuthenticationFilter

    关于cas客户端的基本配置这里就不多说了,不清楚的可以参考上一篇博文:配置简单cas客户端.这里是关于cas客户端实现动态配置认证需要开发说明. 往往业务系统中有些模块或功能是可以不需要登录就可以访问 ...