当前位置:网站首页>729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
729. 我的日程安排表 I / 剑指 Offer II 106. 二分图
2022-07-06 02:00:00 【彼淇梁】
729. 我的日程安排表 I【中等题】【每日一题】
思路:【TreeMap】
学习TreeMap的使用,两种方式。
详见代码注释。
代码1:
class MyCalendar {
TreeMap<Integer,Integer> treeMap;
public MyCalendar() {
treeMap = new TreeMap<>();
}
public boolean book(int start, int end) {
//第一个大于等于 start 的区间
Map.Entry<Integer,Integer> ceilingEntry = treeMap.ceilingEntry(start);
//第一个小于等于 start 的区间
Map.Entry<Integer,Integer> floorEntry = treeMap.floorEntry(start);
//如果 floorEntry 为 null 或者 floorEntry 对应的值(表示区间右端点) 小于等于 start
// 且 同时 ceilingEntry为 null 或者 ceilingEntry 对应的键(表示区间左端点) 大于等于 end
// 那么说明当前这段日程区间 可以插入到treeMap表示的日历中,且不会导致重复预订
if((floorEntry == null || floorEntry.getValue() <= start)
&& (ceilingEntry == null || end <= ceilingEntry.getKey())){
treeMap.put(start,end);
return true;
}
//如果不满足上述条件 那么不能将日常添加到日历中,返回false
return false;
}
}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */
代码2:
class MyCalendar {
TreeMap<Integer,Integer> map;
public MyCalendar() {
//初始化TreeMap,map中key为区间左端点(包含),value为区间右端点(不包含)
map = new TreeMap<>();
//在map中添加整个区间的大边界
//最左侧为-1
map.put(-1,-1);
//最右侧为1000000001
map.put(1000000001,1000000001);
}
public boolean book(int start, int end) {
//求map中 要插入区间左端点 start 左右两侧的值,分别设为left 和right,left表示比start小的区间左端点 right表示比start大的区间左端点
Integer left = map.floorKey(start),right = map.ceilingKey(start);
//如果right值小于end 表示比start大的区间左端点比end小 这时插入[start,end)一定会造成区间重叠,返回false
//如果left对应的区间右端点值比start大 也会造成区间重叠 返回false
if (right < end || map.get(left) > start){
return false;
}
//排除上述情况 区间不会重叠,则将日程成功插入
map.put(start,end);
return true;
}
}
/** * Your MyCalendar object will be instantiated and called as such: * MyCalendar obj = new MyCalendar(); * boolean param_1 = obj.book(start,end); */
剑指 Offer II 106. 二分图【中等题】
思路:【DFS】
读懂题目很重要,题目中对二分图的定义是:
将一个图的所有节点分为两个独立的子集A和B,并使图中每一条边的两个节点一个来自集合A,一个来自集合B。
那么我们可以借用染色的概念来对无向图进行尝试二分,初始时图中的所有节点均为无色状态,从任一节点出发,遍历图中所有节点,如果节点未染色,则将其染为红色,
然后遍历这个节点的所有邻接点,如果邻接点未被染色,则将其染为绿色,并对这个邻接点的所有邻接点进行搜索,未染色则染为红色;如果邻接点已被染色,且与当前节点颜色相同,那么说明染色失败,函数返回false。
详细过程可以看注释。思路及代码参考自官方题解。
代码:
class Solution {
//定义color数组,用来标记无向图中每个节点的颜色
static int[] color;
//定义标志位valid,用于表示当前状态是否有效
static boolean valid;
//定义静态变量用于标记节点颜色
static final int UNCOLORED = 0;
static final int RED = 1;
static final int GREEN = 2;
public boolean isBipartite(int[][] graph) {
int n = graph.length;
//初始化valid为true,表示起始状态有效
valid = true;
//初始化颜色数组color
color = new int[n];
//color赋初值为 无色 UNCOLORED
Arrays.fill(color,UNCOLORED);
//遍历无向图中每个有效节点
for (int i = 0; i < n && valid; i++) {
//如果当前节点未上色,则调用dfs函数将其染为红色
if (color[i] == UNCOLORED){
dfs(i,RED,graph);
}
}
//返回无向图遍历完成后节点的有效状态
return valid;
}
public void dfs(int node,int c,int[][] graph){
//将当前节点node染为指定颜色c
color[node] = c;
//记录当前节点邻接点的目标颜色cNei,如果c为红色,则cNei为绿色,如果c为绿色,则cNei为红色
int cNei = c == RED ? GREEN : RED;
//遍历当前节点的每一个邻接点neighbor
for (int neighbor : graph[node]) {
//如果邻接点无色,则将邻接点染色,即节点为红,则将邻节点染为绿色,节点为绿,则将邻接点染为红色
if (color[neighbor] == UNCOLORED){
//将邻接点染为cNei颜色
dfs(neighbor,cNei,graph);
//如果染完后,发现valid为false,那么染色函数直接返回,不用继续染下一个邻接点了
if (!valid){
return;
}
}else if (color[neighbor] != cNei){
//如果邻接点的颜色已染色,且其颜色不是将要染成的颜色(就是与当前节点颜色相同)
//那么将valid标记为false,表示染色失败。染色函数返回
valid = false;
return;
}
}
}
}
边栏推荐
- MySQL index
- RDD creation method of spark
- dried food! Accelerating sparse neural network through hardware and software co design
- Shutter doctor: Xcode installation is incomplete
- 【网络攻防实训习题】
- Basic operations of database and table ----- set the fields of the table to be automatically added
- Basic operations of database and table ----- delete data table
- Redis key operation
- 0211 embedded C language learning
- genius-storage使用文档,一个浏览器缓存工具
猜你喜欢

Redis-列表

NumPy 数组索引 切片

Using SA token to solve websocket handshake authentication

安装php-zbarcode扩展时报错,不知道有没有哪位大神帮我解决一下呀 php 环境用的7.3

【clickhouse】ClickHouse Practice in EOI
![抓包整理外篇——————状态栏[ 四]](/img/1e/2d44f36339ac796618cd571aca5556.png)
抓包整理外篇——————状态栏[ 四]

UE4 unreal engine, editor basic application, usage skills (IV)

Numpy array index slice

02.Go语言开发环境配置

Overview of spark RDD
随机推荐
How to use C to copy files on UNIX- How can I copy a file on Unix using C?
Force buckle 1020 Number of enclaves
【clickhouse】ClickHouse Practice in EOI
Alibaba canal usage details (pit draining version)_ MySQL and ES data synchronization
Redis-字符串类型
Cookie concept, basic use, principle, details and Chinese transmission
竞价推广流程
Using SA token to solve websocket handshake authentication
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
TrueType字体文件提取关键信息
selenium 等待方式
竞赛题 2022-6-26
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
PHP campus financial management system for computer graduation design
Shutter doctor: Xcode installation is incomplete
Using SA token to solve websocket handshake authentication
02.Go语言开发环境配置
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
MySQL index
剑指 Offer 12. 矩阵中的路径