# Algorithms—57.Insert Interval

/**
* Definition for an interval.
* public class Interval {
*     int start;
*     int end;
*     Interval() { start = 0; end = 0; }
*     Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
int s = newInterval.start;
int e = newInterval.end;
if (intervals.size()==0) {
return intervals;
}
int ns=0;
int ne=0;
boolean beginFlag=true;
boolean endFlag=true;
for (int i = 0; i < intervals.size(); i++) {
if (endFlag&&beginFlag) {
if (intervals.get(i).start>=s) {
ns=s;
beginFlag=false;
}else if (intervals.get(i).end>=s) {
if (intervals.get(i).end>=e) {
endFlag=false;
}else {
ns=intervals.get(i).start;
beginFlag=false;
}
}
}
if (endFlag&&!beginFlag) {
if (intervals.get(i).start>e) {
ne=e;
endFlag=false;
beginFlag=true;
}else if(intervals.get(i).start==e){
ne=intervals.get(i).end;
endFlag=false;
beginFlag=true;
continue;
}else if(intervals.get(i).end>=e){
ne=intervals.get(i).end;
endFlag=false;
beginFlag=true;
continue;
}else if(i==intervals.size()-1){
ne=e;
}
}
if (beginFlag) {
}
}
if (endFlag) {
}
}
}

• 本文已收录于以下专栏：

## LeetCode_57---Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

## lkeetcode 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

## LeetCode 57. Insert Interval/56. Merge Intervals

1. 题目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge ...

## Leetcode57 Insert Interval

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)