Skip to content

Commit 675fcad

Browse files
committed
add 057
1 parent 7da926b commit 675fcad

File tree

6 files changed

+320
-1
lines changed

6 files changed

+320
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ Idx | Date | Question | Python| Java | Domain | Tag | Difficulty | Remark
4848
042|20200627|[108. Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)|[108p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/108_Convert_Sorted_Array_to_Binary_Search_Tree.ipynb)|[108j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/108/Solution.java)|BST|Recursive|Easy
4949
043|20200627|[109. Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/)|[109p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/109_Convert_Sorted_List_to_Binary_Search_Tree.ipynb)|[109j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/109/Solution.java)|BST|Recursive|Medium
5050
044|20200705|[28. Implement strStr()](https://leetcode.com/problems/implement-strstr/)|[028p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/028_Implement_strStr().ipynb)|[028j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/028/Solution.java)|String|KMP|Easy
51-
045|20200816|[56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[056p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/056_Merge_Intervals.ipynb)|[056j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/056/Solution.java)|Sort||Medium
51+
045|20200816|[56. Merge Intervals](https://leetcode.com/problems/merge-intervals/)|[056p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/056_Merge_Intervals.ipynb)|[056j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/056/Solution.java)|Sort||Medium
52+
046|20200816|[57. Insert Interval](https://leetcode.com/problems/insert-interval/)|[057p](https://github.com/shishishu/leetcode-python-java/blob/master/ipynb_files/057_Insert_Interval.ipynb)|[057j](https://github.com/shishishu/leetcode-python-java/blob/master/java_codes/057/Solution.java)|Sort||Hard

images/057_Q.PNG

65.7 KB
Loading

images/057_S1.PNG

22.1 KB
Loading

ipynb_files/057_Insert_Interval.ipynb

Lines changed: 291 additions & 0 deletions
Large diffs are not rendered by default.

java_codes/057/Solution.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.lang.reflect.Array;
2+
import java.util.ArrayList;
3+
4+
class Solution {
5+
public int[][] insert(int[][] intervals, int[] newInterval) {
6+
int start = newInterval[0];
7+
int end = newInterval[1];
8+
ArrayList<int[]> result = new ArrayList<>();
9+
int i = 0;
10+
while (i < intervals.length && intervals[i][1] < start){
11+
result.add(intervals[i]);
12+
i++;
13+
}
14+
while (i < intervals.length && intervals[i][0] <= end){
15+
start = Math.min(start, intervals[i][0]);
16+
end = Math.max(end, intervals[i][1]);
17+
i++;
18+
}
19+
result.add(new int[]{start, end});
20+
while (i < intervals.length){
21+
result.add(intervals[i]);
22+
i++;
23+
}
24+
return result.toArray(new int[result.size()][]);
25+
}
26+
}

java_codes/java_codes.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<sourceFolder url="file://$MODULE_DIR$/109" isTestSource="false" />
5050
<sourceFolder url="file://$MODULE_DIR$/028" isTestSource="false" />
5151
<sourceFolder url="file://$MODULE_DIR$/056" isTestSource="false" />
52+
<sourceFolder url="file://$MODULE_DIR$/057" isTestSource="false" />
5253
</content>
5354
<orderEntry type="inheritedJdk" />
5455
<orderEntry type="sourceFolder" forTests="false" />

0 commit comments

Comments
 (0)