We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bee58af commit eefcf52Copy full SHA for eefcf52
problems/785.is-graph-bipartite.md
@@ -105,6 +105,25 @@ class Solution:
105
- 时间复杂度:$O(N^2)$
106
- 空间复杂度:$O(N)$
107
108
+
109
+如上代码并不优雅,之所以这么写只是为了体现和 886 题一致性。一个更加优雅的方式是不建立 grid,而是利用题目给的 graph(邻接矩阵)。
110
111
+```py
112
+class Solution:
113
+ def isBipartite(self, graph: List[List[int]]) -> bool:
114
+ n = len(graph)
115
+ colors = [0] * n
116
+ def dfs(i, color):
117
+ colors[i] = color
118
+ for neibor in graph[i]:
119
+ if colors[neibor] == color: return False
120
+ if colors[neibor] == 0 and not dfs(neibor,-1*color): return False
121
+ return True
122
+ for i in range(n):
123
+ if colors[i] == 0 and not dfs(i,1): return False
124
125
+ ```
126
127
## 相关问题
128
129
- [886. 可能的二分法](./886.possible-bipartition.md)
0 commit comments