Python for esle

735. 行星碰撞

class Solution:
    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
        q = []
        for x in asteroids: 
            while q and q[-1] > 0 and x < 0:
                if q[-1] <= -x: 
                    y = q.pop()
                    if y == -x: break
                else: break
                
            else: q.append(x)
        return q

1706. 球会落何处

class Solution:
    def findBall(self, grid: List[List[int]]) -> List[int]:
        n = len(grid[0])
        res = [-1] * n         
        for i in range(n):
        	# 一个一个处理
            col = i # 先保存当前球 i 的位置 
            for row in grid:
                direction = row[col] # 方向
                col += direction # 滚动后的位置
                # 出界或前后方向不同,终止内循环。
                if col < 0 or col == n or row[col] != direction: break
            else: res[i] = col # i 球的出口
        return res
class Solution:
    def findBall(self, grid: List[List[int]]) -> List[int]:
        n = len(grid[0])
        # 记录每个球的下标
        res = [i for i in range(n)]
        for row in grid: # 提取行
            for i in range(n):
                if res[i] == -1: continue
                j = res[i] # 当前 i 号球所在的位置
                direction = row[j] # 方问
                j += direction # 当前 i 号球滚动后的位置
                # 已经出界或挡板方向相反
                if j < 0 or j == n or row[j] != direction: res[i] = -1
                else: res[i] = j
        return res

1160. 拼写单词

class Solution:
    def countCharacters(self, words: List[str], chars: str) -> int:
        res = 0
        nums = collections.Counter(chars)
        for word in words:
            word_num = collections.Counter(word)
            for c in word_num:
                if word_num[c] > nums[c]:
                    break;
            else:
                res += len(word)
        return res

1684. 统计一致字符串的数目

class Solution:
    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
        res = 0
        for w in words: # 外循环
            # flag = True
            # for c in w: # 内循环
            #     if c not in allowed:
            #         flag = False
            #         break # 终止(内)循环
            # if flag: res += 1

            # for c in w: # 内循环
            #     if c not in allowed:                    
            #         break # 终止(内)循环
            # else: res += 1 # 这个是和 for 对齐的,跳过 break 终止的循环

            res += all(c in allowed for c in w)
        return res

        return sum(all(c in allowed for c in w) for w in words)
        return sum(set(allowed) >= set(s) for s in words)

2437. 有效时间的数目

class Solution:
    def countTime(self, time: str) -> int:

        def f(s, limit):
            ans = 0
            for i in range(limit):
                # for a, b in zip(s, f"{i:02d}"):
                #     if a not in ['?', b]: break
                # else: ans += 1
                
                # 用 all 代替
                if all(a in ['?', b] for a, b in zip(s, f"{i:02d}")):
                    ans += 1
            return ans

        return f(time[:2], 24) * f(time[3:], 60)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值