{
  "type": "code_options",
  "author": "csdn.net",
  "source": "solution.md",
  "exercise_id": "a6d92383b9494df9871ae265f38784b4",
  "keywords": "数组",
  "title": "插入区间",
  "desc": [
    {
      "content": "\n<p>给你一个<strong> 无重叠的</strong><em> ，</em>按照区间起始端点排序的区间列表。</p>\n<p>在列表中插入一个新的区间，你需要确保列表中的区间仍然有序且不重叠（如果有必要的话，可以合并区间）。</p>\n<p> </p>\n<p><strong>示例 1：</strong></p>\n<pre><strong>输入：</strong>intervals = [[1,3],[6,9]], newInterval = [2,5]<strong><br />输出：</strong>[[1,5],[6,9]]</pre>\n<p><strong>示例 2：</strong></p>\n<pre><strong>输入：</strong>intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]<strong><br />输出：</strong>[[1,2],[3,10],[12,16]]<strong><br />解释：</strong>这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠。</pre>\n<p><strong>示例 3：</strong></p>\n<pre><strong>输入：</strong>intervals = [], newInterval = [5,7]<strong><br />输出：</strong>[[5,7]]</pre>\n<p><strong>示例 4：</strong></p>\n<pre><strong>输入：</strong>intervals = [[1,5]], newInterval = [2,3]<strong><br />输出：</strong>[[1,5]]</pre>\n<p><strong>示例 5：</strong></p>\n<pre><strong>输入：</strong>intervals = [[1,5]], newInterval = [2,7]<strong><br />输出：</strong>[[1,7]]</pre>\n<p> </p>\n<p><strong>提示：</strong></p>\n<ul>\n<li><code>0 <= intervals.length <= 10<sup>4</sup></code></li>\n<li><code>intervals[i].length == 2</code></li>\n<li><code>0 <= intervals[i][0] <= intervals[i][1] <= 10<sup>5</sup></code></li>\n<li><code>intervals</code> 根据 <code>intervals[i][0]</code> 按 <strong>升序</strong> 排列</li>\n<li><code>newInterval.length == 2</code></li>\n<li><code>0 <= newInterval[0] <= newInterval[1] <= 10<sup>5</sup></code></li>\n</ul>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "java"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ],
    [
      {
        "content": "",
        "language": "java"
      }
    ]
  ],
  "template": {
    "content": "\npublic class Interval {\n\tint start;\n\tint end;\n\n\tInterval() {\n\t\tstart = 0;\n\t\tend = 0;\n\t}\n\n\tInterval(int s, int e) {\n\t\tstart = s;\n\t\tend = e;\n\t}\n}\n\nclass Solution {\n\tpublic int[][] insert(int[][] intervals, int[] newInterval) {\n\t\tint[][] newIntervals = new int[intervals.length + 1][];\n\t\tSystem.arraycopy(intervals, 0, newIntervals, 0, intervals.length);\n\t\tnewIntervals[intervals.length] = newInterval;\n\n\t\tArrays.sort(newIntervals, (a, b) -> a[0] - b[0]);\n\t\tStack<int[]> stack = new Stack<>();\n\t\tfor (int[] num : newIntervals) {\n\t\t\tif (stack.isEmpty()) {\n\t\t\t\tstack.push(num);\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tint[] arr = stack.peek();\n\t\t\tif (arr[1] >= num[0]) {\n\t\t\t\tint[] combine = { arr[0], Math.max(arr[1], num[1]) };\n\t\t\t\tstack.pop();\n\t\t\t\tstack.push(combine);\n\t\t\t} else {\n\t\t\t\tstack.push(num);\n\t\t\t}\n\t\t}\n\t\treturn stack.toArray(new int[0][]);\n\t}\n}\n",
    "language": "java"
  },
  "node_id": "dailycode-150f426952d9492f81925cff61127646",
  "license": "csdn.net",
  "created_at": 1637894158,
  "topic_link": "https://bbs.csdn.net/topics/600470920"
}