题目链接

考虑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. spring IOC的常见几种以来注入的方式

    在spring ioc中有三种依赖注入,分别是:a.接口注入:b.setter方法注入:c.构造方法注入: 接口注入: public class ClassA { private InterfaceB ...

  2. 最小二乘法least square

    上研究生的时候接触的第一个Loss function就是least square.最近又研究了一下,做个总结吧. 定义看wiki就够了.公式如下 E(w)=12∑n=1N{y−xWT}2E(w)=12 ...

  3. 在Java Web程序中使用Hibernate

    在Java Web程序中使用Hibernate与普通Java程序一样.本文中将使用Servlet和JSP结合Hibernate实现数据库表的增删改查操作. Web程序中,hibernate.cfg.x ...

  4. 在Spark上运行TopK程序

    1. scala程序如下 package com.cn.gao import org.apache.spark.SparkConf import org.apache.spark.SparkConte ...

  5. Git教程之工作区和暂存区

    工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区:

  6. Strings of Power

    B. Strings of Power Volodya likes listening to heavy metal and (occasionally) reading. No wonder Vol ...

  7. Informatica 常用组件Lookup之四 查找组件

    在映射中配置查找转换时,请定义以下组件: 查找源 端口 属性 条件 元数据扩展 查找源         您可以使用平面文件或关系表作为查找源.创建查找转换时,您可以从以下位置导入查找源: 资料库中的任 ...

  8. C/C++ 语言获取文件大小

    在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明 ...

  9. Faster\Slower 快慢指针的应用

    leetcode很多题目都是利用快慢指针来解决题目,下面具体讲解下快慢指针. 概念: 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍.在循环中的指针移动通常为:fas ...

  10. Android常用http请求框架 简介及现状

    JDK支持的HttpUrlConnection HttpUrlConnection是JDK里提供的联网API,是最原始最基本的API,大多数开源的联网框架基本上也是基于此进行的封装的.HttpUrlC ...