Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f0f7df9

Browse files
update: 57
1 parent af81a75 commit f0f7df9

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

‎.vscode/launch.json‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "启动程序",
11+
"program": "${workspaceFolder}/src/insert-interval/res.js"
12+
}
13+
]
14+
}

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
3737
|49|[Group Anagrams](https://leetcode.com/problems/anagrams/) | [JavaScript](./src/anagrams/res.js)|Medium|
3838
|54|[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js)|Medium|
3939
|55|[Jump Game](https://leetcode.com/problems/jump-game/) <sup>*</sup> | [JavaScript](./src/jump-game/res.js)|Medium|
40+
|57|[Insert Interval](https://leetcode.com/problems/insert-interval/) | [JavaScript](./src/insert-interval/res.js)|Hard|
4041
|66|[Plus One](https://leetcode.com/problems/plus-one/) | [JavaScript](./src/plus-one/res.js)|Easy|
4142
|69|[Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [JavaScript](./src/sqrtx/res.js)|Easy|
4243
|71|[Simplify Path](https://leetcode.com/problems/simplify-path/) | [JavaScript](./src/simplify-path/res.js)|Medium|

‎src/insert-interval/res.js‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {number[][]} intervals
3+
* @param {number[]} newInterval
4+
* @return {number[][]}
5+
*/
6+
var insert = function(intervals, newInterval) {
7+
const len = intervals.length;
8+
if (!len) return [newInterval];
9+
const [start, end] = newInterval;
10+
const new_itervals = [];
11+
12+
let i = 0;
13+
while (i < len && start > intervals[i][0]) {
14+
new_itervals.push(intervals[i]);
15+
i++;
16+
}
17+
18+
new_itervals.push(newInterval);
19+
while (i < len) {
20+
new_itervals.push(intervals[i]);
21+
i++;
22+
}
23+
24+
console.log(new_itervals);
25+
res = [];
26+
i = 0;
27+
while (i < len+1) {
28+
let [left, right] = new_itervals[i];
29+
while (i < len && new_itervals[i+1][0] <= right) {
30+
i += 1;
31+
right = Math.max(new_itervals[i][1], right);
32+
}
33+
res.push([left, right]);
34+
i += 1;
35+
}
36+
37+
return res;
38+
39+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /