Python3 操作二维数组
Leetcode 3202. 找出有效子序列的最大长度 II
from typing import List
class Solution:
def maximumLength(self, nums: List[int], k: int) -> int:
f = [[0] * k for _ in range(k)]
for x in nums:
x %= k
for y, fxy in enumerate(f[x]):
f[y][x] = fxy + 1
return max(map(max, f))
print(Solution().maximumLength([1, 2, 3, 4, 5], 2))
声明m
行n
列二维数组:matrix = [[0] * n for _ in range(m)]
函数map(function, iterable)
:第一个参数为应用于每个元素的函数,第二个参数为可迭代对象