classSolution:defasteroidCollision(self, asteroids: List[int])-> List[int]:
q =[]for x in asteroids:while q and q[-1]>0and x <0:if q[-1]<=-x:
y = q.pop()if y ==-x:breakelse:breakelse: q.append(x)return q
classSolution:deffindBall(self, grid: List[List[int]])-> List[int]:
n =len(grid[0])
res =[-1]* n
for i inrange(n):# 一个一个处理
col = i # 先保存当前球 i 的位置 for row in grid:
direction = row[col]# 方向
col += direction # 滚动后的位置# 出界或前后方向不同,终止内循环。if col <0or col == n or row[col]!= direction:breakelse: res[i]= col # i 球的出口return res
classSolution:deffindBall(self, grid: List[List[int]])-> List[int]:
n =len(grid[0])# 记录每个球的下标
res =[i for i inrange(n)]for row in grid:# 提取行for i inrange(n):if res[i]==-1:continue
j = res[i]# 当前 i 号球所在的位置
direction = row[j]# 方问
j += direction # 当前 i 号球滚动后的位置# 已经出界或挡板方向相反if j <0or j == n or row[j]!= direction: res[i]=-1else: res[i]= j
return res
classSolution:defcountCharacters(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
classSolution:defcountConsistentStrings(self, allowed:str, words: List[str])->int:
res =0for 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
returnsum(all(c in allowed for c in w)for w in words)returnsum(set(allowed)>=set(s)for s in words)
classSolution:defcountTime(self, time:str)->int:deff(s, limit):
ans =0for i inrange(limit):# for a, b in zip(s, f"{i:02d}"):# if a not in ['?', b]: break# else: ans += 1# 用 all 代替ifall(a in['?', b]for a, b inzip(s,f"{i:02d}")):
ans +=1return ans
return f(time[:2],24)* f(time[3:],60)