785. Is Graph Bipartite?从两个集合中取点构图
[抄题]:
Given an undirected graph, return true if and only if it is bipartite.
Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.
The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1. There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.
Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
需要考虑非连通图的情况,此时无法染色 语言:
!validColor(graph, colors, 0, i)
[思维问题]:
如果valid函数中有染色, 那主函数就不用再染 直接用valid函数即可
[一句话思路]:
双色问题的本质是BFS
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 理解下:下一个节点是用其index i表示的
[二刷]:
- “已经染色”用color[i] != -1来表示,同时检查一下有没有染对
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
如果valid函数中有染色, 那主函数就不用再染 直接用valid函数即可
[复杂度]:Time complexity: O(n^2) Space complexity: O(n^2)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
初始化数组:
Arrays.fill(nums, -1);
[算法思想:递归/分治/贪心]:贪心
[关键模板化代码]:
public boolean validColor(int[][] graph, int[] colors, int color, int node) {
//already colored,color again
if (colors[node] != -1) return colors[node] == color;
//color 1, then bfs
colors[node] = color;
for (int next : graph[node]) {
if (!validColor(graph, colors, 1 - color, next)) return false;
}
return true;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean isBipartite(int[][] graph) {
//cc: null
if (graph == null || graph.length == 0) return false;
//ini: colors[] -1
int n = graph.length;
int[] colors = new int[n];
Arrays.fill(colors, -1);
//for loop: not connected graph
for (int i = 0; i < n; i++) {
if (colors[i] == -1 && !validColor(graph, colors, 0, i)) return false;
}
return true;
}
public boolean validColor(int[][] graph, int[] colors, int color, int node) {
//already colored,color again
if (colors[node] != -1) return colors[node] == color;
//color 1, then bfs
colors[node] = color;
for (int next : graph[node]) {
if (!validColor(graph, colors, 1 - color, next)) return false;
}
return true;
}
}
785. Is Graph Bipartite?从两个集合中取点构图的更多相关文章
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- [leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图
Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0, ...
- 785. Is Graph Bipartite?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- sql 查询 一张表里面的数据 在另一张表中是否存在 和 比对两个集合中的差集和交集(原创)
这两天在搞一个修复的小功能 需求: A表,B表,C表,日志文件 先筛选出A表和B表中都符合条件的数据,然后检查这些数据在C表中是否存在.如果不存在,就从日志中读取数据,存入C表中,如果存在,则不做操作 ...
- [LeetCode] 785. Is Graph Bipartite? 是二分图么?
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
- LeetCode 785. Is Graph Bipartite?
原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...
- 查看两个集合中有没有相同的元素的方法。Collections disjoint
在做项目的时候遇到一个种情况,就是要比较两个集合中是否有相同的元素,经过查找资料,找到了Collections类下的disjoint方法下面做的一个小例子: import java.util.Coll ...
- JAVA求集合中的组合
好几个月没弄代码了,今天弄个求组合的DEMO 思路是将集合的每个值对照一个索引,索引大小是集合的大小+2.索引默认为[000...000],当组合后选取的组合值demo为[0100..00].然后根据 ...
随机推荐
- Oracle 11gR2 RAC集群服务启动与关闭总结
引言:这写篇文章的出处是因为我的一名学生最近在公司搭建RAC集群,但对其启动与关闭的顺序和原理不是特别清晰,我在教学工作中也发现了很多学员对RAC知识了解甚少,因此我在这里就把RAC里面涉及到的最常用 ...
- jQuery 性能优化技巧
原文地址:jQuery 性能优化技巧 博客地址:http://www.extlight.com 一.使用最新版本 jQuery 类库 二.合理使用选择器 # 推荐使用 $("#id" ...
- MOSS 2013研究系列---修改默认Logo
开发SharePoint2013 的时候,系统里面有一个“SharePoint” 的logo,客户很少不满意,我们的系统不能出现产品的名称,如下图: 咋么修改呢,咨询了广大网友,给出了一个解决方案: ...
- 打包python文件,让文件程序化
通过对源文件打包,Python程序可以在没有安装 Python的环境中运行,也可以作为一个独立文件方便传递和管理. 现在网上主流的打包方式有两种py2exe或者pyinstaller两款多平台的Pyt ...
- spring事务传播特性实验(2):PROPAGATION_REQUIRED实验结果与分析
本文延续上一文章(spring事务传播特性实验(1):数据准备),在已经准备好环境的情况下,做如下的实验,以验证spring传播特性,加深对spring传播特性的理解. 本次主要验证PROPAGATI ...
- 使用Java读取配置文件
实现起来,相对比较简单,留个备案吧,废话也不多说,请看代码: package com.jd.***.config; import org.junit.*; import java.io.IOExcep ...
- HDU-1937题解
一.题意 一个N*M的矩形,点表示空地,X表示非空地,给你一个数字k,让你在这N*M的区域内找出一个空地数量不小于k且面积最小的矩形.输出矩形的面积. PS:原题的题意是在难懂啊. 二.思路 1.朴素 ...
- node连接mongoDB篇
一般介绍: 由于mongodb数据库在javascript脚本环境中支持bson对象(json对象的二进制形式)的存取,因此对于数据的存取的效率是非常高的.在mongodb数据库中,将每一条等待插入的 ...
- 【洛谷】P2904 [USACO08MAR]跨河River Crossing(dp)
题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when ...
- 【CVE】CVE-2018-4304 Apple多个操作系统函数拒绝服务漏洞
TextImpact: Processing a maliciously crafted text file may lead to adenial of serviceDescription: A ...