LeetCode No.57 InsertInterval
来源:互联网 发布:陈小春新拍电视剧网络 编辑:程序博客网 时间:2024/05/18 15:08
Question: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].
/** * 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> result = new ArrayList<Interval>(); for (Interval i : intervals) { if (newInterval == null || i.end < newInterval.start) result.add(i); else if (i.start > newInterval.end) { result.add(newInterval); result.add(i); newInterval = null; } else { newInterval.start = Math.min(newInterval.start, i.start); newInterval.end = Math.max(newInterval.end, i.end); } } if (newInterval != null) result.add(newInterval); return result; }}
0 0
- LeetCode No.57 InsertInterval
- 【LeetCode】InsertInterval
- LeetCode No.11 ~No.12
- Leetcode NO.78 Subsets
- Leetcode NO.77 Combinations
- Leetcode NO.46 Permutations
- 【leetcode】No. 235 LCABST
- LeetCode no. 35
- Leetcode no. 37
- Leetcode no. 279
- Leetcode no. 335
- Leetcode no. 283
- Leetcode no. 181 (sql)
- Leetcode no. 40
- Leetcode no. 238
- Leetcode no. 26
- Leetcode no. 80
- Leetcode no. 228
- 牛客网模拟(第一场)超级素数
- 常见的动态规划问题分析与求解
- C++11新特性
- 书店管理系统---User模块.Dao层(JavaSE综合运用(一))
- MFC 零碎知识累积
- LeetCode No.57 InsertInterval
- PPP点对点协议中 异步链路中0x7d用作转义字符时的过程
- 2016年蓝桥杯 —— 第七题
- 哪些开源项目值得投注
- Image Classification
- CC2541-修改蓝牙名称和mac地址
- CSS3笔记:滚动、扭曲、旋转、翻转、动画练习整理
- 如何学习好Java语言呢?
- DMP的功能划分