简体   繁体   English

给定一个列表和一个 x 的值,将其拆分,使所有小于 x 的项目排在大于或等于 x 的项目之前

[英]Given a list and a value of x, split it such that all items less than x came before items greater than or equal to x

I have a question.我有个问题。 Is there a more efficient way to solve this issue?有没有更有效的方法来解决这个问题? I made it like below but i created 3 new Lists.我做了如下所示,但我创建了 3 个新列表。

public List<Integer> divideListByX1(List<Integer> list, int x) {
    List<Integer> beforeX = new ArrayList<>();
    List<Integer> afterX = new ArrayList<>();
    List<Integer> all = new ArrayList<>();
    for (Integer integer : list) {
        boolean b = (integer < x) ? beforeX.add(integer) : afterX.add(integer);
    }
    all.addAll(beforeX);
    all.addAll(afterX);
    return all;
}

Thanks for your help谢谢你的帮助

This can be done in-place in linear time using the Pivot operation of Quicksort:这可以使用 Quicksort 的 Pivot 操作在线性时间内就地完成:

public static void pivot(List<Integer> list, int pivot) {
    int left = 0;
    int right = list.size() - 1;

    while (left < right) {
        while (left < list.size() && list.get(left) < pivot) {
            left++;
        }
        while (right >= 0 && list.get(right) >= pivot) {
            right--;
        }
        if (left < right)
            Collections.swap(list, left++, right--);
    }
}

The following way is very slightly more efficient because it adds the numbers straight into the resulting list in one go, instead of using intermediate lists:下面的方法效率稍微高一点,因为它将数字直接添加到一个 go 的结果列表中,而不是使用中间列表:

public static List<Integer> divideListByX1(List<Integer> list, int x) {
    Integer[] result = new Integer[list.size()];
    int lowIndex = 0;
    int highIndex = result.length - 1;
    for (Integer n : list) {
        if (n < x)
            result[lowIndex++] = n;
        else
            result[highIndex--] = n;
    }
    return Arrays.asList(result);
}

The downside with that is that the "high numbers" part is not in the original order of the input list, but I guess that's not important.这样做的缺点是“高数字”部分不是输入列表的原始顺序,但我想这并不重要。

Also, here's a way to do it using Streams, but is probably not more efficient than your initial attempt:另外,这里有一种使用 Streams 的方法,但可能不会比您最初的尝试更有效:

public List<Integer> divideListByX1(List<Integer> list, int x) {
    return list
        .stream()
        .collect(Collectors.partitioningBy(n -> n >= x))
        .values()
        .stream()
        .flatMap(List::stream)
        .toList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列出所有小于或等于X的变量 - List all variables that are less than or equal to X 将列表分为给定的相等大小X,其余列表不应小于大小Y - Split List into Given Equal Size X and remaining list should not be less than size Y 结合hasNextInt和大于/小于X的值 - Combine hasNextInt and value greater/less than X checks 如何断言列表中至少有n个项大于x(在junit中使用hamcrest) - How to assert that a list has at least n items which are greater than x (with hamcrest in junit) Java Hashmap:获取大于X值的所有键 - Java Hashmap: get all keys greater than X value 如何对 Flux 中的项目进行计数,如果计数大于 X,则返回错误,否则继续流水线 - How to Count Items in a Flux, return error if count is greater than X, else continue with Pipeline 如何查找兄弟索引小于x且大于y的元素 - How to find elements whose sibling index is less than x and greater than y Selenium2 Java-检查页面上的值是否大于X - Selenium2 Java - Check if value on page is greater than X 在O(1)中找到数组值大于x的第一个索引 - find first index in array value greater than x in O(1) 查找第一个元素大于或等于最后一个元素且子数组大小等于 X 的子数组 - Find subarrays with first element greater than or equal to last element and the subarray size equal to X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM