java编程练习题九

题目连接:https://leetcode-cn.com/problems/is-graph-bipartite/

 

public class Solution785 {
	public boolean isBipartite(int[][] graph) {
        int[] color = new int[graph.length];
        for(int i=0; i<graph.length; ++i){
            if(color[i]==0){
                color[i]=1;
                if(!dfs(graph,color,i))
                    return false;
            }
        }
        return true;
	}
    public boolean dfs(int[][] graph, int[] color, int x){
        for(int i:graph[x]){
            if(color[i]==color[x])
                return false;
            else if(color[i]==0){ 
                color[i]=-color[x];
                if(!dfs(graph,color,i)) 
                    return false;
            }
        }
        return true;
    }
}

 

 

 

 

 

 

 

 

 

 

版权声明:本文为qq_44923097原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_44923097/article/details/105141825