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 d2b0613

Browse files
Add Insert Interval
1 parent 04cad79 commit d2b0613

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Solutions in various programming languages are provided. Enjoy it.
1919
10. [Bulls and Cows](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/10-Bulls-and-Cows)
2020
11. [Maximum Product Subarray](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/11-Maximum-Product-Subarray)
2121
12. [Combination Sum III](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/12-Combination-Sum-III)
22+
13. [Insert Interval](https://github.com/AlgoStudyGroup/Leetcode/tree/master/September-LeetCoding-Challenge/13-Insert-Interval)
2223

2324
## August LeetCoding Challenge
2425
Click [here](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/) for problem descriptions.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
public class Solution {
2+
public int[][] Insert(int[][] intervals, int[] newInterval) {
3+
var res = new List<int[]>();
4+
foreach(int[] i in intervals){
5+
if(newInterval == null || i[1] < newInterval[0]){
6+
res.Add(i);
7+
}
8+
else if(i[0] > newInterval[1]){
9+
res.Add(newInterval);
10+
res.Add(i);
11+
newInterval = null;
12+
}
13+
else{
14+
newInterval[0] = Math.Min(newInterval[0], i[0]);
15+
newInterval[1] = Math.Max(newInterval[1], i[1]);
16+
}
17+
}
18+
if(newInterval != null){
19+
res.Add(newInterval);
20+
}
21+
return res.ToArray();
22+
}
23+
}

0 commit comments

Comments
(0)

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