题目链接

考虑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. (转)SQL Server 列转行

    原文:http://www.myexception.cn/sql-server/1078985.html1,2,3,4,5以上是一个字符串或则一逗号分隔的数字. 这里希望用一条语句查询出这样的效果: ...

  3. Top N的MapReduce程序MapReduce for Top N items

    In this post we'll see how to count the top-n items of a dataset; we'll again use the flatland book ...

  4. Java实现对Mysql的图片存取操作

    1.MySQL中的BLOB类型 Mysql中可以存储大文件数据,一般使用的BLOB对象.如图片,视频等等. BLOB是一个二进制大对象,可以容纳可变数量的数据.因为是二进制对象,所以与编码方式无关.有 ...

  5. mybatis 针对SQL Server 的 主键id生成策略

    SQL Server中命令: select newId()  ,可以得到SQL server数据库原生的UUID值,因此我们可以将这条指令写到 Mybatis的主键生成策略配置selectKey中. ...

  6. Quartz2D-二维画图引擎 、自己定义UI控件

    // // MyDraw.m // 绘图 #import "MyDraw.h" @implementation MyDraw //Quartz2D 是一个二维绘图引擎 //自己定义 ...

  7. 廖雪峰的python学习网址

    http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00140737570055886 ...

  8. 历尽折腾,终于把Unity3D 的demo发布安卓啦(问题)

    只要碰到两个比较蛋疼的问题: 1. Error generating final archive: Debug certificate expired on **** 从字面了解,是由于Debug证书 ...

  9. javascript深度克隆对象

    /** * * @param obj * @returns {*} */ //深度克隆 function cloneObject(obj) { if (obj === null || typeof(o ...

  10. UE查找和替换技巧实例

    1 删除多余的空行 如果是在WORD中,则查找^p^p替换为^p. 如果是在EXCEL里,则为全部选中,然后点击编辑,定位,定位条件,空值. 将全部选中空白的行,如图所示 再次点击编辑,删除,删除整行 ...