中级农民
- 积分
- 111
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-10-10
- 最后登录
- 1970-1-1
|
注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
- class Solution {
-
- public int[][] merge(int[][] intervals) {
-
- if (intervals.length <= 1)
- return intervals;
- // Sort by ascending starting point
- Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
- List<int[]> result = new ArrayList<>();
-
- int[] newInterval = intervals[0];
-
- result.add(newInterval);
-
- for (int[] interval : intervals) {
- if (interval[0] <= newInterval[1]) // Overlapping intervals, move the end if needed
- newInterval[1] = Math.max(newInterval[1], interval[1]);
- else { // Disjoint intervals, add the new interval to the list
- newInterval = interval;
- result.add(newInterval);
- }
- }
- int[][] newResult = new int[result.size()][2];
-
- result.toArray(newResult);
-
- // return result.toArray(new int[0][0]);
- return newResult;
- }
- }
复制代码
代码如上,
如果我不用31行,换成30行,还是可以运行。
大家理解这句吗? result.toArray(new int[0][0]); 为啥是一个0大小的2d array呢?
求米求米
|
上一篇: 亚麻OA最近一道高频新题 "购物模式"下一篇: 【实验抄题策略】笨办法抄题打卡
|