Difficult:Medium
題目
You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.
Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).
Return intervals after the insertion.
翻譯
給定一個非重疊區間的數組,其中 interval[i] = [starti, endi] 表示第 i 個區間的開始和結束,區間按 starti 升序排序。您還會得到一個間隔 newInterval = [start, end],它表示另一個間隔的開始和結束。
將 newInterval 插入到區間中,以便區間仍然按 starti 升序排序,並且區間仍然沒有任何重疊區間(如有必要,合併重疊區間)。
插入後的返回間隔。
範例
Example 1
1 | Input: intervals = [[1,3],[6,9]], newInterval = [2,5] |
Example 2
1 | Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] |
解題思路
1.將要插入的陣列跟原本的陣列做比較
2.紀錄開始的地方和要刪除的數量
Solution
Code 1 :
1 | var insert = function (intervals, newInterval) { |