LeetCode 56 - Merge Intervals

AC Solution

class Solution {
    public int[][] merge(int[][] intervals) {
        if (intervals == null || intervals.length <= 1) {
            return intervals;
        }
        Arrays.sort(intervals, (i1, i2) -> Integer.compare(i1[0], i2[0]));
        List<int[]> result = new ArrayList<>();
        int[] tmpInterval = intervals[0];
        result.add(tmpInterval);
        for (int[] interval : intervals) {
            if (interval[0] <= tmpInterval[1]) {
                tmpInterval[1] = Math.max(tmpInterval[1], interval[1]);
            } else {
                tmpInterval = interval;
                result.add(tmpInterval);
            }
        }
        return result.toArray(new int[result.size()][]);
    }
}

Summary and Reflection

LeetCode 56 & 57 changed function interface, into a two-dimensional array. This algorithm is not difficult questions, emphasis on the coding skills, bug-free and clean and beautiful is not easy. This question is also important to note that since Java 8, should use lambda expressions in an interview obviously saves time, it is time to abandon Comparatorthe.

Tonight listened nine chapters explain the headlines in spring 2020 strokes of pen questions, ACM Gold Gangster really ease and to various algorithms come in handy, for me this chicken dish players need to constantly sum up + exercise. One thing you can not pay more attention to daily drop points, that is, variable naming , as a ~ 2 years working experience in social trick, then i, j which can easily be despised, of course, still be used for recycling. There is a complex function can be split, to write a very long giant function may not look good. ACM Great God level have no hope in this life, to the extent that Cheng Yun also left hanging, can catch up with the pace of Hunan God enough for me it!

Published 11 original articles · won praise 2 · Views 665

Guess you like

Origin blog.csdn.net/liheng301/article/details/105001718