给定若干连续的有序区间(默认是升序),将有交集的区间合并成一个连续的区间。
举例:
输入:[[1,5],[3,7],[4,8],[10,12],[11,16],[18,20]]
输出:[[1,8],[10,16],[18,20]]
解决代码:
public static void main(String[] args) { String a = "[[1,5],[3,7],[4,8],[10,12],[11,16],[18,20]]"; List<List> list = JSONUtil.toList(JSONUtil.parseArray(a), List.class); List<List> returnList = new ArrayList<>(); List<Integer> canUpdateList = list.get(0); for (int i = 1; i < list.size(); i++) { Integer listFirstNum = canUpdateList.get(0); Integer listSecNum = canUpdateList.get(1); List<Integer> objectList = list.get(i); Integer firstNumA = objectList.get(0); Integer secNumA = objectList.get(1); if (listFirstNum < firstNumA && listSecNum > firstNumA && listSecNum < secNumA) {