defgardenNoAdj(self, n: int, paths: List[List[int]]): # type: ignore res = [0] * n # 建立矩阵 G = [[] for _ inrange(n)] for e in paths: G[e[0] - 1].append(e[1] - 1) G[e[1] - 1].append(e[0] - 1) # pop随机弹出结果 for i inrange(n): res[i] = ({1, 2, 3, 4} - {res[j] for j in G[i]}).pop() return res
注意
花园从1开始排序,注意数组索引的变化
python中集合可以pop()
hinak0
Recording personal notes, covering various aspects.