• 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)
    
    
  • 相关阅读:
    YOLO2 (2) 测试自己的数据
    Ubuntu 14.04服务器配置 (1) 安装和配置
    window10+linux双系统安装
    机械纪元 尼奥
    如何标数据
    usb-cam (3)摄像机标定文件-ORB-SLAM标定文件
    ORB-SLAM2(3) ROS下实时跑ORB_SLAM2
    usb-cam(1)安装
    usb-cam (2)摄像机标定
    Linux下的压缩zip,解压缩unzip命令详解及实例
  • 原文地址:https://www.cnblogs.com/weiyinfu/p/7574197.html
Copyright © 2020-2023  润新知