当前位置:网站首页>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;
}
边栏推荐
- Introduction to Sichuan Tuwei ca-is3082w isolated rs-485/rs-422 transceiver
- [batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
- 【蒸馏】PointDistiller: Structured Knowledge DistillationTowards Efficient and Compact 3D Detection
- C Getting Started tutorial
- RTKLIB-b33版本中GALILEO广播星历存储问题
- Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
- Chuantu microelectronics breaks through the high-end isolator analog chip market with ca-is3062w
- Mysql database import SQL file display garbled code
- Five causes of PCB board deformation and six solutions 2021-10-08
- Misunderstanding of switching triode
猜你喜欢
JMeter introduction practice ----- use of global variables and local variables
Without "rice", you can cook "rice". Strategy for retrieving missing ground points under airborne lidar forest using "point cloud intelligent mapping"
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Tempest HDMI leak receive 1
一次弄清楚 Handler 可能导致的内存泄漏和解决办法
Introduction to Sichuan Tuwei ca-is3082wx isolated rs-485/rs-422 transceiver
海思3559 sample解析:vio
【批处理DOS-CMD命令-汇总和小结】-cmd扩展命令、扩展功能(cmd /e:on、cmd /e:off)
【批处理DOS-CMD命令-汇总和小结】-应用程序启动和调用、服务和进程操作命令(start、call、)
ts环境搭建
随机推荐
Shell tips (134) simple keyboard input recorder
OAuth 2.0一键登录那些事
Tuwei Digital Isolator and interface chip can perfectly replace imported brands Ti and ADI
Manufacturing process of PCB 2021-10-11
VectorDraw Web Library 10.10
Chuantu microelectronics high speed and high performance rs-485/422 transceiver series
【Qt】快捷键
LeetCode 每日一题——515. 在每个树行中找最大值
Research on 3D model retrieval method based on two channel attention residual network - Zhou Jie - paper notes
[batch dos-cmd command - summary and summary] - file and directory operation commands (MD, RD, xcopy, dir, CD, set, move, copy, del, type, sort)
Home environment monitoring system design (PC version) (mobile app version to be determined)
图扑软件数字孪生 3D 风电场,智慧风电之海上风电
Modular programming of wireless transmission module nRF905 controlled by single chip microcomputer
Ca-is1200u current detection isolation amplifier has been delivered in batch
无“米”,也能煮“饭”利用“点云智绘”反演机载LiDAR林下缺失地面点攻略
npm install 报错 : gyp ERR! configure error
Modular programming of oled12864 display controlled by single chip microcomputer
Sichuan Tuwei ca-is3105w fully integrated DC-DC converter
Sichuan earth microelectronics high performance, high integration and low cost isolated 485 transceiver
How comfortable it is to use Taijiquan to talk about distributed theory!