标签:list alt bsp 和我 style inf for code rap
题目描述:
第一次提交:通过76/78 留坑
class Solution: def isBipartite(self, graph: List[List[int]]) -> bool: dic = {} l = [] for i in range(len(graph)): if graph[i] != []: dic[i] = 0 l.append(i) break tmp = 1 while l: print(l) for i in range(len(l)): for node in graph[l.pop(0)]: if node not in dic: dic[node] = tmp l.append(node) elif dic[node] != tmp: return False tmp = 0 if tmp == 1 else 1 return True
改:DFS O(N+M) O(N)
class Solution: def isBipartite(self, graph) -> bool: dye = defaultdict(int) def DFS(s, color): dye[s] = color for i in graph[s]: # 已经上色了,如果和我一样表示不行 if dye[i] == color: return False # 没上色就染成和我相反的 if not dye[i]: if not DFS(i, -color): return False return True for i in range(len(graph)): # 没被染色的就继续染色 if not dye[i]: if not DFS(i, 1): return False return True
标签:list alt bsp 和我 style inf for code rap
原文地址:https://www.cnblogs.com/oldby/p/13328968.html