如何将这个代码片段(for循环和其中的逻辑,主要包括for循环)转换为函数式Java 8

aelbi1ox  于 2023-10-14  发布在  Java
关注(0)|答案(3)|浏览(116)

我有以下片段

public int eraseOverlapIntervals(int[][] intervals) {
        Comparator<int []> c = (e1,e2)->Integer.compare(e1[0],e2[0]);

        Arrays.sort(intervals,c);
        // Get max non overlapping intervals
        int count = 1;
        int []current = intervals[0];
        for(int i = 1; i<intervals.length; i++) {
            int []next = intervals[i];
            if(current[1]<=next[0]) {
                current = next;
                count++;
            } else {
                if(current[1]>next[1]) {
                    current = next;
                }
            }
        }
        return intervals.length-count;
    }
}

如何以纯函数式Java-8方式重写它?

n3ipq98p

n3ipq98p1#

请花点时间在文档上,探索Java 8的功能,因为这样你会得到解决方案,但你不会学习。看看下面的解决方案,它可能会帮助你:

public static void main(String[] args) {
    int[][] intervals = {{1, 3}, {2, 4}, {5, 7}, {6, 8}};

    int count = eraseOverlapIntervals(intervals);

    System.out.println("The number of non-overlapping intervals is: " + count);
}

public static int eraseOverlapIntervals(int[][] intervals) {
    // Sort the intervals by their start values
    Arrays.sort(intervals, Comparator.comparingInt(e -> e[0]));
    AtomicInteger count = new AtomicInteger(1);
    final int[][] current = {intervals[0]};
    Arrays.stream(intervals, 1, intervals.length)
            .forEach(next -> {
                if (current[0][1] <= next[0]) {
                    current[0] = next;
                    count.getAndIncrement();
                } else if (current[0][1] > next[1]) {
                    current[0] = next;
                }
            });

    return intervals.length - count.get();
}
xjreopfe

xjreopfe2#

请尝试以下操作。

int eraseOverlapIntervals(int[][] intervals) {
    // Get max non overlapping intervals
    class Current { int []current; }
    final Current c = new Current() {{ current = intervals[0]; }};
    return
        intervals.length -
        (int) Stream.of(intervals)
                    .sorted(Comparator.comparingInt(e -> e[0]))
                    .skip(1)
                    .filter(x -> {
                        if(c.current[1] <= x[0]) {
                            c.current = x;
                            return true;
                        } else if (c.current[1] > x[1])
                            c.current = x;
                        return false;
                    })
                    .count();
}
b1uwtaje

b1uwtaje3#

您可以使用Java 8中引入的Stream API以函数式方式编写。这段代码已经用函数重新写好了,希望对你的例子有帮助:

public int eraseOverlapIntervals(int[][] intervals) {
       Comparator<int[]> comparator = Comparator.comparingInt(e -> e[0]);
        Arrays.sort(intervals, comparator);
    
        final int[] current = { intervals[0][0], intervals[0][1] };
        final int[] result = { 0 };
        
        Arrays.stream(intervals)
            .skip(1)
            .forEach(next -> {
                if (current[1] <= next[0]) {
                    current[0] = next[0];
                    current[1] = next[1];
                } else {
                    result[0]++;
                    if (current[1] > next[1]) {
                        current[0] = next[0];
                        current[1] = next[1];
                    }
                }
            });
        
        return intervals.length - result[0];

}

我已经重新编辑了我的回答诚实编译错误。我希望这个答案将有所帮助

相关问题