当前位置: 首页 > 知识库问答 >
问题:

java[001,002,003]如何效率最高的穷举出两位以上的所有组合方式?

墨宜人
2023-06-13

java 现有list1[11,33,22]
效率最高穷举出两位以上任意相连的方式,如:

[11,33]、[11,22]、[11,33,22]、[11,22,33]、
[33,22]、[33,11]、[33,11,22]、[33,22,11]、
[22,11]、[22,33]、[22,33,11]、[22,11,33]、

共有2个答案

燕航
2023-06-13
import java.util.*;

public class Test {

    // 使用递归实现
    public static void main(String[] args) {
        int[] nums = { 11, 33, 22 };
        for (int i = 2; i <= nums.length; i++) {
            combine(nums, new int[i], 0, 0);
        }
    }

    public static void combine(int[] nums, int[] temp, int start, int index) {
        if (index == temp.length) {
            permutation(temp, 0, temp.length - 1);
            return;
        }
        for (int i = start; i < nums.length; i++) {
            temp[index] = nums[i];
            combine(nums, temp, i + 1, index + 1);
        }
    }

    public static void permutation(int[] arr, int start, int end) {
        if (start == end) {
            System.out.println(Arrays.toString(arr));
        } else {
            for (int i = start; i <= end; i++) {
                swap(arr, start, i);
                permutation(arr, start + 1, end);
                swap(arr, start, i);
            }
        }
    }

    public static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
穆彬郁
2023-06-13

跑一下吧:

import java.util.*;

public class Main {
    public static void main(String[] args) {
        List<Integer> list1 = Arrays.asList(11,33,22);
        List<List<Integer>> res = enumerate(list1);
        System.out.println(res);
    }

    private static List<List<Integer>> enumerate(List<Integer> list) {
        List<List<Integer>> res = new ArrayList<>();

        if (list.size() <= 1) {
            return res;
        }

        for (int i = 0; i < list.size() - 1; i++) {
            Integer num1 = list.get(i);
            for (int j = i + 1; j < list.size(); j++) {
                Integer num2 = list.get(j);
                List<Integer> subList = list.subList(i, j + 1);
                res.add(subList);

                List<List<Integer>> subRes = enumerate(subList);
                for (List<Integer> sub : subRes) {
                    res.add(sub);
                }
            }
        }

        return res;
    }
}
 类似资料:
  • 使用Spark,我的算法的中间步骤之一将输出(键、向量)到pairrdd。在这一步完成之后,我希望生成所有可能的键的2-组合,并对它们的值执行进一步的操作,即我希望有一个带有((Key1,Key2),(Vector1,Vector2))的PairRDD。 关于如何以可伸缩的方式实现这一点,有什么想法吗?我想不通。谢谢!!

  • 问题内容: 我想找到设置为的最高有效位。我已经从尝试一切可能的方式来进行或运算所有的位从到和它不工作。 就像我想拥有一样。 问题答案: 如果您坚持直接使用按位运算符,则可以尝试如下操作: 我们将掩码初始化为,因为它表示1后跟31 0。我们使用该值来测试索引31(第32个点)是否为1。当将此值与一起使用时,除非在中设置了相应的位,否则将得到0 。如果是这种情况,我们返回。如果不是,则将掩码向右移动1

  • a = [i for i in range(1, 8000)] 假如有个这样的列表, 我需要把里面的所有值组合 然后求 组合的总和与100差的最小值。 例如 1和2组合 1+2 =3 与100 差 3-100 == -97 , 1和3组合 1+3-100 = -96 , 1和4组合,1+4-100=-95..... 1+99-100=0 ....依次类推, 1+2+3+4+5+6...+7999-

  • 本文向大家介绍如何提高组件的渲染效率呢?相关面试题,主要包含被问及如何提高组件的渲染效率呢?时的应答技巧和注意事项,需要的朋友参考一下 function Child({seconds}){ console.log('I am rendering'); return ( I am update every {seconds} seconds ) }; export default React.mem

  • 我有一个关于使用“永远”类型的穷举开关/情况的问题。 比如说,我有一组字符串:{a,B}(字符串可以是任意长的单词,而且集合本身可能非常大),对于每个子集(比如{},{a,B}),我想创建一个函数:show:Set= 预发伪代码: 是否有可能在编译时保证show函数中包含所有可能的子集?所以把C加到集合{A,B,C}需要我扩充show函数吗?并为{C}、{A,C}、{B,C}和{A,B,C}添加案

  • 我想有效地计算一个n位数字(在我的例子中,n=36)的所有组合,并且设置了精确的k位。