[LeetCode-interview algorithm classic-Java implementation] [057-Insert Interval (Insert Interval)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [057-Insert Interval (Insert Interval)], leetcode -- java
[057-Insert Interval (Insert Interval )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary ).
You may assume that the intervals were initially sorted according to their start times.
Example 1:
Given intervals[1,3],[6,9], Insert and merge[2,5]In[1,5],[6,9].
Example 2:
Given[1,2],[3,5],[6,7],[8,10],[12,16], Insert and merge[4,9]In[1,2],[3,10],[12,16].
This is because the new interval[4,9]Overlaps[3,5],[6,7],[8,10].

Theme

Inserts a new range for a series of non-covered intervals. If necessary, the intervals are merged starting from the start time.

Solutions

If the original interval is smaller than the inserted interval, insert the result set. If the inserted interval overlaps, update the inserted interval. If the inserted interval is smaller than the original interval, insert the inserted interval first, and then add the increased interval.

Code Implementation

Algorithm Implementation class

Import java. util. using list; import java. util. list; public class Solution {public List <Interval> insert (List <Interval> intervals, Interval newInterval) {// List of sets for saving results <Interval> result = new parameter List <> (); // if (intervals! = Null) {// traversal element for (Interval item: intervals) {// newInterval = null indicates that the inserted interval has been processed. // Add the interval smaller than the inserted interval to the result set if (newInterval = null | item. end <newInterval. start) {result. add (item) ;}// add an interval greater than the inserted interval to the result set, and add the inserted interval to the result set else if (item. start> newInterval. end) {result. add (newInterval); result. add (item); newInterval = null;} // The insert interval overlaps and the else {newInterval is updated. start = Math. min (newInterval. start, Item. start); newInterval. end = Math. max (newInterval. end, item. end) ;}}// if the inserted interval is not empty, the inserted interval is not processed. if (newInterval! = Null) {result. add (newInterval) ;}return result ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the source for reprinting at http://blog.csdn.net/derrantcm/article/details/47164431]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.