LeetCode – Refresh – Insert Inverval

Similar to merge intervals. But this is easier than merge interval, because every side is kind of "sorted".

 1 /**
 2  * Definition for an interval.
 3  * struct Interval {
 4  *     int start;
 5  *     int end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
13         vector<Interval> result;
14         if (intervals.size() == 0) {
15             result.push_back(newInterval);
16             return result;
17         }
18         queue<int> left, right;
19         int len = intervals.size();
20         bool lf = true, rf = true;
21         for (int i = 0; i < len; i++) {
22             if (lf && newInterval.start < intervals[i].start) {
23                 left.push(newInterval.start);
24                 lf = false;
25             }
26             if (rf && newInterval.end < intervals[i].end) {
27                 right.push(newInterval.end);
28                 rf = false;
29             }
30             left.push(intervals[i].start);
31             right.push(intervals[i].end);
32         }
33         if (lf) left.push(newInterval.start);
34         if (rf) right.push(newInterval.end);
35         while (!left.empty()) {
36             int s = left.front(), e = right.front();
37             left.pop(), right.pop();
38             while (!left.empty()) {
39                 if (e >= left.front()) {
40                     e = right.front();
41                     left.pop(), right.pop();
42                 } else break;
43             }
44             result.push_back(Interval(s, e));
45         }
46         return result;
47     }
48 };
时间: 2024-10-05 16:36:29

LeetCode – Refresh – Insert Inverval的相关文章

LeetCode --- 57. Insert Interval

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

LeetCode——Search Insert Position

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6], 2

[LeetCode] Search Insert Position [21]

题目 Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,6]

[leetcode]_Search Insert Position

题目:查找元素target插入一个数组中的位置. 代码: public int searchInsert(int[] A, int target) { int len = A.length; int i; for(i = 0 ; i < len ; i++){ if(target <= A[i]) break; } return i; } 简单的让人难以置信.吼吼~ [leetcode]_Search Insert Position,布布扣,bubuko.com

LeetCode: Search Insert Position [034]

[题目] Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5,6], 5 → 2 [1,3,5,

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

【Leetcode】Insert Delete GetRandom O(1) - Duplicates allowed

题目链接:https://leetcode.com/problems/insert-delete-getrandom-o1-duplicates-allowed/ 题目: Design a data structure that supports all following operations in average O(1) time. Note: Duplicate elements are allowed. insert(val): Inserts an item val to the c

Java for LeetCode 057 Insert Interval

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