当前位置:网站首页>57. 插入区间
57. 插入区间
2022-06-25 06:40:00 【Mr Gao】
57. 插入区间
给你一个 无重叠的 ,按照区间起始端点排序的区间列表。
在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。
示例 1:
输入:intervals = [[1,3],[6,9]], newInterval = [2,5]
输出:[[1,5],[6,9]]
示例 2:
输入:intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
输出:[[1,2],[3,10],[12,16]]
解释:这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。
示例 3:
输入:intervals = [], newInterval = [5,7]
输出:[[5,7]]
示例 4:
输入:intervals = [[1,5]], newInterval = [2,3]
输出:[[1,5]]
示例 5:
输入:intervals = [[1,5]], newInterval = [2,7]
输出:[[1,7]]
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes) {
*returnSize = 0;
int left = newInterval[0];
int right = newInterval[1];
bool placed = false;
int** ans = malloc(sizeof(int*) * (intervalsSize + 1));
*returnColumnSizes = malloc(sizeof(int*) * (intervalsSize + 1));
for (int i = 0; i < intervalsSize; ++i) {
int* interval = intervals[i];
if (interval[0] > right) {
// 在插入区间的右侧且无交集
if (!placed) {
int* tmp = malloc(sizeof(int) * 2);
tmp[0] = left, tmp[1] = right;
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
placed = true;
}
int* tmp = malloc(sizeof(int) * 2);
memcpy(tmp, interval, sizeof(int) * 2);
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
} else if (interval[1] < left) {
// 在插入区间的左侧且无交集
int* tmp = malloc(sizeof(int) * 2);
memcpy(tmp, interval, sizeof(int) * 2);
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
} else {
// 与插入区间有交集,计算它们的并集
left = fmin(left, interval[0]);
right = fmax(right, interval[1]);
}
}
if (!placed) {
int* tmp = malloc(sizeof(int) * 2);
tmp[0] = left, tmp[1] = right;
(*returnColumnSizes)[*returnSize] = 2;
ans[(*returnSize)++] = tmp;
}
return ans;
}
边栏推荐
- Tempest HDMI leak receive 2
- Estimation of dense forest volume based on LIDAR point cloud with few ground points
- 国外LEAD域名邮箱获取途径
- Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute
- Distributed quorum NWR of the alchemy furnace of the Supreme Master
- Can I open a stock account with a compass? Is it safe?
- WinForm实现窗口始终在顶层
- Hisilicon 3559 sample parsing: Vio
- OAuth 2.0一键登录那些事
- [Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)
猜你喜欢
![Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]](/img/ea/3eba7e4a433b0c501f9b207641dc6a.jpg)
Notes: [open class] neural network and deep learning -- tensorflow2.0 actual combat [Chinese course]
![Insert and sort the linked list [dummy unified operation + broken chain core - passive node]](/img/2a/ccb1145d2b4f9fbd8d0812deace93b.png)
Insert and sort the linked list [dummy unified operation + broken chain core - passive node]
![[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905](/img/a7/fc5d2f4640322a5d7222cce83c8898.jpg)
[single chip microcomputer project training] multipoint temperature wireless acquisition system based on nRF905

Harmony food menu interface

How to use printf of 51 single chip microcomputer

The method of judging whether triode can amplify AC signal

Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes

How comfortable it is to use Taijiquan to talk about distributed theory!

“空间转换”显著提升陡崖点云的地面点提取质量

Leetcode daily question - 515 Find the maximum value in each tree row
随机推荐
[batch dos-cmd command - summary and summary] - commands related to Internet access and network communication (Ping, Telnet, NSLOOKUP, ARP, tracert, ipconfig)
差点被这波Handler 面试连环炮带走~
【批处理DOS-CMD命令-汇总和小结】-文件与目录操作命令(md、rd、xcopy、dir、cd、set、move、copy、del、type、sort)
Explain distributed raft with dynamic diagram
对链表进行插入排序[dummy统一操作+断链核心--被动节点]
Unity3D邪门实现之GUI下拉菜单Dropdown设计无重复项
C reads XML on the web
[Batch dos - cmd Command - Summary and Summary] - cmd extension Command, extension Function (CMD / E: on, CMD / E: off)
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
RTKLIB-b33版本中GALILEO广播星历存储问题
Estimation of dense forest volume based on LIDAR point cloud with few ground points
[leetcode] two num · sum of two numbers
Chuantuwei ca-is3720lw alternative material No. iso7820fdw
Full range of isolator chips with integrated isolated power supply
OAuth 2.0一键登录那些事
指南针可以开股票账户吗?安全吗?
[batch dos-cmd command - summary and summary] - application startup and call, service and process operation commands (start, call, and)
音频(五)音频特征提取
AttributeError: ‘Upsample‘ object has no attribute ‘recompute_ scale_ factor‘
Elk + filebeat log parsing, log warehousing optimization, logstash filter configuration attribute