注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 匿名 于 2021-9-9 16:14 编辑
linkedin 內部hr聯絡的 剛剛面完 current project 問滿細的一題很像merge interval的自己定義class 還要return total interval 的length
public class Segment {
private List<int[]> segments;
private int totalLength;
public Segment() {
segments = new ArrayList<>();
totalLength = 0;
}
// 1. scan through the segments
// 2. insert it, merge if necessary
// O(n)
public void add(int[] newSegment) {
if(seg == null || seg.length != 2) {
throw new Exceptions(& result.add(segments.get(i));
i++;
}
segments = result;
}
// return total len
public int totalLength() {
return totalLength;
}
// |______]
// [_____]
// |_________________] [____]
// 1. [begin, end]
// 2. List<int[start, end]>
}
|