当前位置: 首页 > news >正文

leetcode56--合并数组

1. 题意

将给定区间合并为不相交的若干个区间。
合并数组

2. 题解

先进行排序,再根据是否相交进行合并。

2.1 我的代码
class Solution {
public:struct int_cmp {static bool cmp(const vector<int> &a, const vector<int> &b) {if (a[0] != b[0])return a[0] < b[0];return a[1] < b[1];}};bool isOverlap(const vector<int> &a,const vector<int> &b) {if (a.empty() || b.empty())return true;return a[1] >= b[0];}vector<vector<int>> merge(vector<vector<int>>& intervals) {sort(intervals.begin(), intervals.end(), int_cmp::cmp);vector<vector<int>> ans;vector<int> cur;int cnt = 0;for (auto &interval: intervals) {   //std::cout << interval[0] << ":" << interval[1] << std::endl;if ( isOverlap(cur, interval) ) {if (cur.empty())cur = interval;else  {cur[1] = max(cur[1], interval[1]);}}else {ans.emplace_back(cur);cur = interval;}}ans.emplace_back(cur);return ans;}
};
2.2 桶排序

跟括号匹配一样的思路

class Solution {public:vector<vector<int>> merge(vector<vector<int>>& intervals) {int n = intervals.size();int left = 10000, right = 0;for (vector<int>& interval : intervals) {left = min(left, interval[0]);right = max(right, interval[1]);}vector<int> buckets(right-left+1, 0);vector<bool> existed(right-left+1, false);for (vector<int>& interval : intervals) {buckets[interval[0]-left]++;buckets[interval[1]-left]--;existed[interval[0]-left] = true;existed[interval[1]-left] = true;}int cnt = 0, last = -1;vector<vector<int>> res;for (int i = left; i <= right; i++) {cnt += buckets[i-left];if (cnt == 0) {if (last != -1) {res.push_back({last, i});last = -1;} else if (existed[i-left]) {res.push_back({i, i});}} else if (last == -1) {last = i;}}return res;}
};作者:明天更要加油啊
链接:https://leetcode.cn/problems/merge-intervals/solutions/2678187/bi-guan-fang-ti-jie-geng-you-xiu-de-onji-zw2x/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章:

  • Maven学习记录
  • 根据疾病名生成病例prompt
  • centos7 如何安装UI
  • 《QT实用小工具·一》电池电量组件
  • Windows 最佳文件管理器:快速、简单、直观、自由 | 开源日报 No.175
  • x86架构中的寄存器和常用指令
  • FlinkSQL之Flink SQL Join二三事
  • <el-table>设置一列为固定字段,其他列为循环生成
  • 星光/宝骏/缤果/长安 车机CarPlay手机操作破解教程V2.0版本(无需笔记本、无需笔记本、无需笔记本)
  • 各大pdf转word软件都用的哪家的ocr引擎?
  • 详解IOS的Automatically Sign在设备上打包
  • MySQL索引18连问,谁能顶住
  • Hbase 王者荣耀数据表 HBase常用Shell命令
  • 目标检测——中国交通标志数据集
  • JAVA 100道题(22)
  • AWS实战 - 利用IAM对S3做访问控制
  • Centos6.8 使用rpm安装mysql5.7
  • FastReport在线报表设计器工作原理
  • Hibernate最全面试题
  • HTML中设置input等文本框为不可操作
  • Java 网络编程(2):UDP 的使用
  • Java反射-动态类加载和重新加载
  • JAVA之继承和多态
  • js 实现textarea输入字数提示
  • Markdown 语法简单说明
  • select2 取值 遍历 设置默认值
  • vue:响应原理
  • Vue2.0 实现互斥
  • 机器学习 vs. 深度学习
  • 近期前端发展计划
  • 我建了一个叫Hello World的项目
  • 我这样减少了26.5M Java内存!
  • ​虚拟化系列介绍(十)
  • ![CDATA[ ]] 是什么东东
  • #pragma 指令
  • #stm32驱动外设模块总结w5500模块
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (floyd+补集) poj 3275
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (转)setTimeout 和 setInterval 的区别
  • .net6解除文件上传限制。Multipart body length limit 16384 exceeded
  • .NET应用架构设计:原则、模式与实践 目录预览
  • @column注解_MyBatis注解开发 -MyBatis(15)
  • @WebService和@WebMethod注解的用法
  • [BJDCTF2020]The mystery of ip1
  • [C#]C#学习笔记-CIL和动态程序集
  • [Docker]四.Docker部署nodejs项目,部署Mysql,部署Redis,部署Mongodb
  • [Flutter]设置应用包名、名称、版本号、最低支持版本、Icon、启动页以及环境判断、平台判断和打包
  • [Gym-102091E] How Many Groups
  • [NISACTF 2022]easyssrf
  • [RK-Linux] 移植Linux-5.10到RK3399(四)| 检查HDMI配置与打开内核LOGO显示
  • [Symbol.toPrimitive](hint) hint 什么时候为 default?
  • [UE4]Montage动画设置Slot
  • [Unity]出android包出错:java.nio.file.accessdeniedexception
  • [Unity独立/合作开发]实现背包系统中物品的拾取拖拽掉落还有换位置