1221. 分割平衡字符串
class Solution:
def balancedStringSplit(self, s: str) -> int:
ans, d = 0, 0
for ch in s:
if ch == 'L':
d += 1
else:
d -= 1
if d == 0:
ans += 1
return ans
1252. 奇数值单元格的数目
class Solution:
def oddCells(self, m: int, n: int, indices: List[List[int]]) -> int:
rows = [0] * m
cols = [0] * n
for x, y in indices:
rows[x] += 1
cols[y] += 1
oddx = sum(row % 2 for row in rows)
oddy = sum(col % 2 for col in cols)
# 奇加偶,偶加奇
return oddx * (n - oddy) + (m - oddx) * oddy
1260. 二维网格迁移
一维展开
class Solution:
def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]:
m, n = len(grid), len(grid[0])
ans = [[0] * n for _ in range(m)]
for i, row in enumerate(grid):
for j, v in enumerate(row):
index1 = (i * n + j + k) % (m * n)
ans[index1 // n][index1 % n] = v
return ans
1379. 找出克隆二叉树中的相同节点
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
if original is None or original is target:
return cloned
return self.getTargetCopy(original.left, cloned.left, target) or \
self.getTargetCopy(original.right, cloned.right, target)
796

被折叠的 条评论
为什么被折叠?



