- 浏览: 184794 次
- 性别:
- 来自: 济南
文章分类
最新评论
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].
题目中给定了一个排好序的interval数组,interval之间没有覆盖,然后插入一个新的interval,merge之后返回,从第一个元素开始,如果有冲突更新newInterval的start,将它加入结果集中,然后判断newInterval的end属性。遍历一遍数组,时间复杂度为O(n)。代码如下:
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].
题目中给定了一个排好序的interval数组,interval之间没有覆盖,然后插入一个新的interval,merge之后返回,从第一个元素开始,如果有冲突更新newInterval的start,将它加入结果集中,然后判断newInterval的end属性。遍历一遍数组,时间复杂度为O(n)。代码如下:
/** * 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 List<Interval> insert(List<Interval> intervals, Interval newInterval) { List<Interval> list = new ArrayList<Interval>(); if(intervals == null || intervals.size() == 0) { list.add(newInterval); return list; } int i = 0; while(i < intervals.size() && intervals.get(i).end < newInterval.start) { list.add(intervals.get(i)); i ++; } if(i < intervals.size()) newInterval.start = Math.min(newInterval.start, intervals.get(i).start); list.add(newInterval); while(i < intervals.size() && newInterval.end >= intervals.get(i).start) { newInterval.end = Math.max(newInterval.end, intervals.get(i).end); i ++; } while (i < intervals.size()) { list.add(intervals.get(i)); i ++; } return list; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 269Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 271You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 388Given a string s and a dictiona ... -
Merge Intervals
2016-03-07 05:25 503Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 568Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 482Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 666Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 473The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 433Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 582Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 590Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 429All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 905Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 933Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 606Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 691Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 857Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 789You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 723For a undirected graph with tre ... -
Bulb Switcher
2016-02-28 12:12 442There are n bulbs that are init ...
相关推荐
java java_leetcode题解之Insert Interval.java
12 Insert Interval 45 13 Two Sum 47 14 Two Sum II Input array is sorted 49 15 Two Sum III Data structure design 51 16 3Sum 53 17 4Sum 55 18 3Sum Closest 57 19 String to Integer (atoi) 59 20 Merge ...
js js_leetcode题解之57-insert-interval.js
在本资源包中,我们关注的是C语言的学习以及如何通过LeetCode平台进行编程练习,特别是针对第57题“插入区间”(Insert Interval)的解题方法。LeetCode是一个在线平台,提供了各种算法题目,旨在帮助程序员提升技能...
接着,点击“Number Ranges”,然后点击“Change Intervals”,最后点击“Insert Interval”。 二、测试号码范围对象 可以使用函数模块 NUMBER_GET_NEXT 来获取号码范围对象的下一个可用号码。下面是一个测试程序...
第57题,"插入区间"(Insert Interval),是其中一道挑战性较高的问题,旨在检验开发者对区间操作、数据结构以及排序算法的理解。下面将详细探讨这个问题的背景、解题策略以及相关的C++编程知识点。 问题描述: ...
* Insert Interval:该题目要求将一个区间插入到另一个区间中,实现方法使用了排序和迭代算法。 三、字符串和动态规划 * String to Integer (atoi):该题目要求将字符串转换成整数,实现方法使用了状态机算法。 * ...
10. Insert Interval 区间插入是一个数组问题,要求将一个区间插入到已经排序的区间数组中。可以使用二分搜索来解决该问题。 11. Two Sum 两数之和是一个经典的数组问题,要求找到数组中两个元素之和等于目标值的...
5. **Merge Intervals(using (Insert Interval) ).cpp** - 这是第56题“合并区间”的解冑,涉及到区间操作,尤其是如何有效地插入新区间并合并重叠区间。 6. **Integer to Roman.cpp** - 第40题,与“Roman to ...
5. InsertInterval - 在一组区间中插入一个新的区间,并确保结果区间仍然是排序和无重叠的。这需要理解区间的概念,并运用排序和插入算法。 6. PartitionLabels - 将字符串划分为尽可能多的部分,使得每个字母...
- Insert Interval: 在一组已经排序的区间中,插入一个新的区间。 - Rotate List: 给定一个链表的头节点head,当旋转了k个位置后,返回链表的新头节点。 - Unique Paths / Unique Paths II: 前者计算从矩阵的左上角...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
颜色分类leetcode leetcode.etc My solutions of the problems in Online judge website, leetcode, lintcode, etc. leetcode: 13 ...Insert Interval Easy Two Strings Are Anagrams(比较字符串) E
Interval 解决方法:遍历 LeetCode: 229. Majority Element II 解决方法:Majority Voting算法的变种。但是最终的算法实现形式,很难理解。 2018-08-19 19:16 LeetCode: 79. Word Search 解决方法:DFS LeetCode: 31...
LifeInterval57Insert Interval56Merge Intervals252Meeting Rooms253Meeting客房II352Data流从数据Stream53Maximum Subarray325Maximum大小子阵总和脱节IntervalsTreeMapCounter239Sliding窗口Maximum295Find中位数...
4. 插入区间:`tree.insert(intervalStart, intervalEnd, data)`,其中`intervalStart`和`intervalEnd`是区间的起始和结束,`data`是关联的数据。 5. 查询:`tree.search(intervalStart, intervalEnd)`,返回与查询...
在这个项目中,头文件可能包含了`IntervalTree`类的定义,其中包括构造函数、析构函数以及之前提到的`insert`、`query`、`remove`等成员函数的声明。 区间树在实际应用中有很多用途,例如在数据库索引、日程安排、...
INSERT INTO experiment VALUES (1, 'Busted urban myth', '01-JUN-2006 02:00:00 PM', INTERVAL '1 2:31:15.1250' DAY(1) TO SECOND(4)); ``` 查询实验结束时间: ```sql SELECT experiment_id, experiment_start,...