【LeetCode】Merge Intervals Insert Interval

2024-08-25 12:18

本文主要是介绍【LeetCode】Merge Intervals Insert Interval,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1、Merge Intervals
Total Accepted: 6989 Total Submissions: 34958 My Submissions
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
注意给的区间可能是无序的,先排序后处理。
基本的思路还是比较合并吧,比较考察思维逻辑,注意边界处理。

Java AC

/*** Definition for an interval.* public class Interval {*     int start;*     int end;*     Interval() { start = 0; end = 0; }*     Interval(int s, int e) { start = s; end = e; }* }*/
public class Solution {public ArrayList<Interval> merge(ArrayList<Interval> intervals) {if(intervals == null){return intervals;}int size = intervals.size();if(size == 0 || size == 1){return intervals;}Collections.sort(intervals, new Comparator<Interval>() {public int compare(Interval o1, Interval o2) {if (o1.start == o2.start) {return o1.end - o2.end;}return o1.start - o2.start;}});ArrayList<Interval> list = new ArrayList<Interval>();Interval interval = intervals.get(0);int start = interval.start;int end = interval.end;for(int i = 1; i < size; i++){Interval intval = intervals.get(i);if(intval.start <= end){int tempSize = list.size();if (tempSize > 0) {list.remove(tempSize-1);}start = Math.min(start, intval.start);end = Math.max(end, intval.end);}else{if (i == 1) {list.add(new Interval(start, end));}start = intval.start;end = intval.end;}list.add(new Interval(start, end));}return list;}
}
2、Insert Interval 

Total Accepted: 6691 Total Submissions: 33506 My Submissions
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].
求解给定的区间在已知区间的位置,将给定区间放入已知区间,然后就回到1的处理方式。

Java AC

/*** Definition for an interval.* public class Interval {*     int start;*     int end;*     Interval() { start = 0; end = 0; }*     Interval(int s, int e) { start = s; end = e; }* }*/
public class Solution {public ArrayList<Interval> insert(ArrayList<Interval> intervals,Interval newInterval) {ArrayList<Interval> list = new ArrayList<Interval>();if (intervals == null || intervals.size() == 0) {list.add(newInterval);return list;}int pos = searchInsert(intervals, newInterval.start);return merge(intervals, pos, newInterval);}public ArrayList<Interval> merge(ArrayList<Interval> intervals, int pos, Interval newInterval) {if(intervals == null){return intervals;}int size = intervals.size();ArrayList<Interval> newList = new ArrayList<Interval>();if (pos == 0) {newList.add(newInterval);newList.addAll(intervals);}else if (pos == size) {newList.addAll(intervals);newList.add(newInterval);}else {int i = 0;while (i < size) {if (i == pos) {newList.add(newInterval);}newList.add(intervals.get(i));i++;}}return merge(newList);}public ArrayList<Interval> merge(ArrayList<Interval> intervals) {int size = intervals.size();ArrayList<Interval> list = new ArrayList<Interval>();Interval interval = intervals.get(0);int start = interval.start;int end = interval.end;for(int i = 1; i < size; i++){Interval intval = intervals.get(i);if(intval.start <= end){int tempSize = list.size();if (tempSize > 0) {list.remove(tempSize-1);}start = Math.min(start, intval.start);end = Math.max(end, intval.end);}else{if (i == 1) {list.add(new Interval(start, end));}start = intval.start;end = intval.end;}list.add(new Interval(start, end));}return list;}public int searchInsert(ArrayList<Interval> intervals, int target) {int size = intervals.size();if (target < intervals.get(0).start) {return 0;}if (target > intervals.get(size - 1).end) {return size;}int low = 0;int high = size - 1;int mid = 0;while (low <= high) {mid = (low + high) >> 1;if (intervals.get(mid).start > target) {high = mid - 1;} else if (intervals.get(mid).start < target) {low = mid + 1;} else {return mid;}}return low;}
}

这篇关于【LeetCode】Merge Intervals Insert Interval的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1105529

相关文章

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return