当前位置:网站首页>LeetCode56. 合并区间
LeetCode56. 合并区间
2022-07-27 07:00:00 【Java全栈研发大联盟】
题目传送地址: https://leetcode.cn/problems/merge-intervals/
运行效率:
代码如下:
class Solution {
public static int[][] merge(int[][] intervals) {
//先将区间项进行排序 因为区间项有可能是这样{
{1,4},{0,4}}; 排序成{
{0,4},{1,4}}
Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
//处理边界情况
if(intervals.length==1){
return intervals;
}
List<int[]> list = new ArrayList<>();
int intervalStart = intervals[0][0]; //区间起点
int intervalEnd = intervals[0][1]; //区间结尾
for (int i = 1; i < intervals.length; i++) {
int start = intervals[i][0];
int end = intervals[i][1];
if (intervalEnd >= start) {
//重叠情况 当前遍历项和区间出现重叠
intervalEnd = Math.max(intervalEnd, end); } else {
// 无重叠情况
int[] interval = new int[2];
interval[0] = intervalStart;
interval[1] = intervalEnd;
list.add(interval);
intervalStart = start;
intervalEnd = end;
}
if(i==intervals.length-1){
//遍历到最后一个区间项了
int[] interval = new int[2];
interval[0] = intervalStart;
interval[1] = intervalEnd;
list.add(interval);
}
}
int[][] result = new int[list.size()][];
result = list.toArray(result);
return result;
}
}
边栏推荐
- Chapter 6 Shell Logic and Arithmetic
- 利用 Amazon DynamoDB 和 Amazon S3 结合 gzip 压缩,最大化存储玩家数据
- Flink principle (I) TTL management and fault tolerance mechanism of state
- Using loops to process data in tables in kettle
- 杂谈:高考
- Jjwt generate token
- 【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
- ARP broadcasting practice cases
- (2022 Niuke multi school III) a-ancestor (LCA)
- Examples of Oracle triggers
猜你喜欢
![Multithreading [preliminary - Part 1]](/img/e6/5808bbdb8368bc780bde4c151e7cd7.png)
Multithreading [preliminary - Part 1]

Introduction to network -- overview of VLAN and trunk

Codeforces Round #810 (Div.2) A-C

Use Amazon dynamodb and Amazon S3 combined with gzip compression to maximize the storage of player data

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

ADC噪声全面分析 -01- ADC噪声的类型以及ADC特性

Clickhouse notes 1 | introduction, features | installation and use based on centos7 system | common data types | mergetree table engine | SQL operation

Array method and loop in JS

帮忙发一份招聘,base全国,有兴趣的可以过来看看

ARP广播实践案例
随机推荐
Oracle cleans up the Database disk space of tables with referenced partitions
单片机多级菜单
Basic functions and collections of guava
Summary of several common ways to join dimension tables in Flink
Quickly update the information in a field in kettle
The solution of using sqlplus to display Chinese as garbled code
Panabit SNMP配置
Use reflection to dynamically modify annotation attributes of @excel
(2022 Hangdian multi school III) 1009.package delivery (greedy)
Temperature and humidity measurement and display device based on Arduino
网络入门——vlan及trunk概述
STM32_ Find the cause of entering hardfault_ Handler's function
Understanding and learning of properties class and properties configuration file
IDEA中文乱码怎么办
Introduction to network -- overview of VLAN and trunk
shell 函数和数组练习
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
闭散列和开散列解决哈希冲突
Multi condition query of when
电子量产项目框架--基本思想