1. 程式人生 > >LeetCode | Insert Interval(插入區間)

LeetCode | 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 [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].


題目解析:

題目很簡單,如果新插入的範圍能夠使原始範圍合併,就合併之。但要注意分情況:整體在i結點前,整體在i結點後,new.start<i.start 以及new.start>=i.start。考慮全情況以後,程式碼就好寫了。

由於設定了一個新的容器,而不是基於原容器修改的,所以到最後要判斷newInterval是否已經插入到res中。

class Solution {
public:
    vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
        vector<Interval> res;

        int len = intervals.size();
        bool flag = false;  //判斷newInterval是否已經被插入到res中,如果沒有的話,在迴圈結束後插入新的區域

        for(int i = 0;i < len;i++){
            //如果newInterval在i前面就直接插入,然後將後續的結點插入返回
            if(newInterval.end < intervals[i].start){
                flag = true;
                res.push_back(newInterval);
                while(i < len)
                    res.push_back(intervals[i++]);
                break;
            }
            //如果new整體在後面,插入當前結點i,繼續向後找
            if(newInterval.start > intervals[i].end){
                res.push_back(intervals[i]);
                continue;
            }
            //當new.start在i.start前面時
            if(newInterval.start < intervals[i].start && newInterval.end >= intervals[i].start){
                newInterval.end = max(newInterval.end,intervals[i].end);
                continue;
            }
            //當new.start在i.start...i.end之間時
            if(newInterval.start >= intervals[i].start && newInterval.start <= intervals[i].end){
                newInterval.start = intervals[i].start;
                newInterval.end = max(newInterval.end,intervals[i].end);
            }
        }
        if(!flag)
            res.push_back(newInterval);
        return res;
    }
};


相關推薦

LeetCode | Insert Interval插入區間

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the inter

insert into插入資料

向表中新增資料 Insert into 簡單語法形式: Insert into 表名[(列名[,列名]...)] values(值 [,值],...); Values 後面的值的排列要與into子句

Leetcode演算法——57、插入區間insert interval

給定一系列不重複的區間,要求將一個新的區間插入到這些集合中(如果有必要,則合併)。 假設這些區間已經根據起始位置排好序了。 示例1: 輸入: intervals = [[1,3],[6,9]], newInterval = [2,5] 輸出: [[1,5],[6,9]]

LeetCode 715. Range Module / 57. Insert Interval區間查詢更新,離散化

A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the following interfaces in an efficient manner.

LeetCode 35. 搜尋插入位置 Search Insert PositionC語言

題目描述: 給定一個排序陣列和一個目標值,在陣列中找到目標值,並返回其索引。如果目標值不存在於陣列中,返回它將會被按順序插入的位置。 你可以假設陣列中無重複元素。 示例 1: 輸入: [1,3,5,6], 5 輸出: 2 示例 2: 輸入: [1

[LeetCode] 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 initiall

InnoDB Insert Buffer插入緩沖

ring discard const 檢查 inno 鏈表 二級 今天 並且 InnoDB Insert Buffer(插入緩沖) 每個存儲存儲引擎自身都有自己的特性(決定性能以及更高可靠性),而InnoDB的關鍵特性有: 插入緩沖(Insert Buffer)--》C

leetcode-35 search-insert-position(搜尋插入位置

這是我第一道通過自己獨立思考通過的程式碼,雖然題目簡單,但是還是很激動!先看一下題目描述: 題目描述的很清楚,一定要仔細讀題,直接上程式碼: 1 public int searchInsert(int[] nums, int target) { 2 int j = 0

LeetCode:56. Merge Intervals合併區間

Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Ex

Leetcode 147 Insertion Sort List插入排序

解題思路:模擬插入排序,注意指標的變換即可。/** * Definition for singly-linked list. * struct ListNode { * int val;

每天一算法 -- 插入排序

代碼實現 順序 oid 第一個 min 選擇排序 [] 簡單 -- 一、原理   插入排序就是把當前待排序的元素插入到一個已經排好序的列表裏面。對於給定的一組記錄,初始時假定第一個記錄自成一個有序序列,其余記錄為無序序列。接著從第二個記錄開始,按照記錄的大小依次將當前處理

LeetCode Insert Interval

dcl star pos clas res intervals efi result lines LeetCode解題之Insert Interval 原題 給出多個不重合的數據區段,如今插入一個數據區段。有重合的區段要進行合並。 註意點:

在LaTex中插入電路圖的方法插入圖片

插入 exp order ring alt 表示 target index strong 主要的需求是要在文檔中插入電路圖。 有兩種方法,一種是直接在LaTex中繪制電路圖,使用的庫主要是circ和circuitikz 一、直接在LaTex中繪制電路圖

插入刪除

bool 裏的 輸入 default main system heap move parent 用堆實現優先級隊列,插入和刪除都很快o(logN)編程語言中的內存堆與這裏的數據結構是不一樣的堆:一種樹(特殊的二叉樹)特點:它是完全二叉樹,除了樹的最後一層節點不需要是滿,其他

leetcode記錄貼go語言

problems 空間換時間 方法 follow nil 一次 code turn make 沒事的時候打算開始玩一玩leetcode,不然天天寫代碼,卻對算法沒啥認識還是有點尷尬的。雖說是做題,其實大部分就是為了看看別人牛逼的思路。盡量每天一題把~ 1.兩數之和 給定一

python排序插入排序 從小到大順序

range def brush lis pop break pri highlight python def insert_sort(ilist): for i in range(len(ilist)): for j in range(i):

Leetcode程式設計練習C++實現

7、反轉整數 /* 題目描述: 給定一個 32 位有符號整數,將整數中的數字進行反轉。 基本思想: 1、類似於字串的逆置,取x的最低位(個位)數字:pop = x % 10; 2、求結果: rev = rev * 10 + pop; 3、將 x 更新為: x

LeetCode——反轉整數Reverse Integer

給定一個 32 位有符號整數,將整數中的數字進行反轉。 示例 1: 輸入: 123 輸出: 321  示例 2: 輸入: -123 輸出: -321 示例 3: 輸入: 120 輸出: 21 注意: 假設我們的環境只能儲存 32 位有符號整數

35. Search Insert Positionpython+cpp

題目: 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 wer

leetcode 刷題java版本

1、leetcode 1 給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] + nums[1] = 2