【算法】力扣第 263 场周赛

发布时间:2022-06-27 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了【算法】力扣第 263 场周赛脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

5902. 检查句子中的数字是否递增

python就是好用,splIT()划分+dic计数

class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        dic={str(i):1 for i in range(101)}
        res=0
        for x in s.split():
            if dic.get(x):
                if res<int(x):
                    res=int(x)
                else:
                    return False
        
        return True
@H_960_126@

5903. 简易银行系统

从没遇到过这么简单的模拟

class Bank:

    def __init__(self, balance: List[int]):
        self.m=[*balance]
        self.n=len(self.m)


    def transfer(self, account1: int, account2: int, money: int) -> bool:
        account1-=1
        account2-=1
        if account1<0 or account1>=self.n or account2<0 or account2>=self.n:return False
        if self.m[account1]>=money:
            self.m[account1]-=money
            self.m[account2]+=money
            return True
        else:
            return False


    def deposit(self, account: int, money: int) -> bool:
        account-=1
        if account<0 or account>=self.n:return False
        self.m[account]+=money
        return True


    def withdraw(self, account: int, money: int) -> bool:
        account-=1
        if account<0 or account>=self.n:return False
        if self.m[account]>=money:
            self.m[account]-=money
            return True
        else:
            return False



# Your Bank object will be instantiated and called as such:
# obj = Bank(balance)
# param_1 = obj.transfer(account1,account2,money)
# param_2 = obj.deposit(account,money)
# param_3 = obj.withdraw(account,money)

5904. 统计按位或能得到最大值的子集数目

史上最简单T3之一,combination->过

class Solution:
    def countMaxOrSubsets(self, nums: List[int]) -> int:
        def findxor(nums):
            n=len(nums)
            PRe=nums[0]
            for i in range(1,n):
                pre|=nums[i]
            return pre
        
        dic=defaultdict(int)
        n=len(nums)
        res=-1
        for a in range(n):
            for i in combinations(nums, a+1):
                    tmp=findxor(i)
                    res=max(res,tmp)
                    dic[tmp]+=1
        return dic[res]

脚本宝典总结

以上是脚本宝典为你收集整理的【算法】力扣第 263 场周赛全部内容,希望文章能够帮你解决【算法】力扣第 263 场周赛所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。