Java版排列组合工具类 - Java Permutation and Combination Tools


( All code listed in this article is included in my personal lib, and the repo is hosted at: https://github.com/raistlic/LibRaistCommon )


最近在整理个人代码,有些觉得可能有用的,拿出来共享一下 大笑


先上用法示例代码:


问题一: 有三个字符串 "a", "b", "c",进行排列,列出共有多少种排列方式

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo permutation :");
    for(List<String> list : Permutation.of(Arrays.asList("a", "b", "c")))
      System.out.println(list);
  }
}
运行效果:

run:
===== demo permutation :
[a, b, c]
[a, c, b]
[b, a, c]
[b, c, a]
[c, a, b]
[c, b, a]
BUILD SUCCESSFUL (total time: 0 seconds)


问题二: 从五个数 1, 2, 3, 4, 5 中任取 3 个数,列出共有多少种取法

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo combination :");
    for(List<Integer> list : Combination.of(Arrays.asList(1, 2, 3, 4, 5), 3))
      System.out.println(list);
  }
}
运行效果:
run:
===== demo combination :
[1, 2, 3]
[1, 2, 4]
[1, 2, 5]
[1, 3, 4]
[1, 3, 5]
[1, 4, 5]
[2, 3, 4]
[2, 3, 5]
[2, 4, 5]
[3, 4, 5]
BUILD SUCCESSFUL (total time: 0 seconds)


问题三: 从五个数 1, 2, 3, 4, 5 中任取 3 个数进行排列,列出共有多少种排列方式

public class PNCDemo {
  
  public static void main(String[] args) {
    
    System.out.println("===== demo both :");
    for(List<Integer> list : Permutation.of(Arrays.asList(1, 2, 3, 4, 5), 3))
      System.out.println(list);
  }
}
运行效果:

run:
===== demo both :
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 1, 2]
[3, 2, 1]
[1, 2, 4]
[1, 4, 2]
[2, 1, 4]
[2, 4, 1]
[4, 1, 2]
[4, 2, 1]
[1, 2, 5]
[1, 5, 2]
[2, 1, 5]
[2, 5, 1]
[5, 1, 2]
[5, 2, 1]
[1, 3, 4]
[1, 4, 3]
[3, 1, 4]
[3, 4, 1]
[4, 1, 3]
[4, 3, 1]
[1, 3, 5]
[1, 5, 3]
[3, 1, 5]
[3, 5, 1]
[5, 1, 3]
[5, 3, 1]
[1, 4, 5]
[1, 5, 4]
[4, 1, 5]
[4, 5, 1]
[5, 1, 4]
[5, 4, 1]
[2, 3, 4]
[2, 4, 3]
[3, 2, 4]
[3, 4, 2]
[4, 2, 3]
[4, 3, 2]
[2, 3, 5]
[2, 5, 3]
[3, 2, 5]
[3, 5, 2]
[5, 2, 3]
[5, 3, 2]
[2, 4, 5]
[2, 5, 4]
[4, 2, 5]
[4, 5, 2]
[5, 2, 4]
[5, 4, 2]
[3, 4, 5]
[3, 5, 4]
[4, 3, 5]
[4, 5, 3]
[5, 3, 4]
[5, 4, 3]
BUILD SUCCESSFUL (total time: 0 seconds)


话说算法这种东西,似乎天生适合写成“策略接口” ———— 夫算法者,策略者也。


第一个文件: 排列和组合都要用到的阶乘类(原类名没有改,去掉了与本题无关的部分)

/*
 * Copyright 2012 wuyou ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.math.BigInteger;

/**
 * This class
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com.hexiang.utils.arrange; public class TryCombination{ public static void main(String[] args){ System.out.println("对整数数组进行组合:C(n,n)"); int[] intArray=new int[4]; for(int i=0;i<intArray.length;i++){ intArray[i]=i+1; } System.out.println("对整数数组进行组合:C(4,4)"); Combination intCombination1=new Combination(intArray.length); while(intCombination1.hasMore()){ int[] index=intCombination1.getNext(); for(int i=0;i<intArray.length;i++){ if(index[i]!=0){ System.out.print(intArray[index[i]*i]+" "); } } System.out.println(); } System.out.println("对整数数组进行组合:C(4,3)"); Combination intCombination2=new Combination(intArray.length,3); while(intCombination2.hasMore()){ int[] index=intCombination2.getNext(); for(int i=0;i<intArray.length;i++){ if(index[i]!=0){ System.out.print(intArray[index[i]*i]+" "); } } System.out.println(); } String str="abcd"; char[] chArray=str.toCharArray(); System.out.println("对字符数组进行组合:C(4,4)"); Combination strCombination1=new Combination(chArray.length); while(strCombination1.hasMore()){ int[] index=strCombination1.getNext(); for(int i=0;i<chArray.length;i++){ if(index[i]!=0){ System.out.print(chArray[index[i]*i]+" "); } } System.out.println(); } System.out.println("对字符数组进行组合:C(4,3)"); Combination strCombination2=new Combination(chArray.length,3); while(strCombination2.hasMore()){ int[] index=strCombination2.getNext(); for(int i=0;i<chArray.length;i++){ if(index[i]!=0){ System.out.print(chArray[index[i]*i]+" "); } } System.out.println(); } } }
可以使用递归来实现字符串的排列组合。具体步骤如下: 1. 定义一个递归函数,传入当前已经排列好的字符串和剩余待排列的字符串。 2. 如果待排列字符串为空,则输出当前已经排列好的字符串。 3. 否则,依次将待排列字符串中的每个字符加入已排列字符串中,并递归调用自身,直到待排列字符串为空。 下面是一个示例代码,可以枚举出1-n的所有组合,然后随机选取任意多个。 ```java import java.util.ArrayList; import java.util.List; import java.util.Random; public class PermutationCombination { public static void main(String[] args) { int n = 5; List<String> list = new ArrayList<>(); for (int i = 1; i <= n; i++) { list.add(String.valueOf(i)); } List<String> result = new ArrayList<>(); permutation("", list, result); System.out.println("所有组合:"); for (String s : result) { System.out.println(s); } System.out.println("\n随机选取任意多个:"); Random random = new Random(); for (int i = 0; i < 5; i++) { int num = random.nextInt(result.size()); System.out.println(result.get(num)); } } private static void permutation(String prefix, List<String> list, List<String> result) { if (list.isEmpty()) { result.add(prefix); } else { for (int i = 0; i < list.size(); i++) { List<String> newList = new ArrayList<>(list); String newPrefix = prefix + newList.remove(i); permutation(newPrefix, newList, result); } } } } ``` 输出结果如下: ``` 所有组合: 12345 12354 12435 12453 12534 12543 13245 13254 13425 13452 13524 13542 14235 14253 14325 14352 14523 14532 15234 15243 15324 15342 15423 15432 21345 21354 21435 21453 21534 21543 23145 23154 23415 23451 23514 23541 24135 24153 24315 24351 24513 24531 25134 25143 25314 25341 25413 25431 31245 31254 31425 31452 31524 31542 32145 32154 32415 32451 32514 32541 34125 34152 34215 34251 34512 34521 35124 35142 35214 35241 35412 35421 41235 41253 41325 41352 41523 41532 42135 42153 42315 42351 42513 42531 43125 43152 43215 43251 43512 43521 45123 45132 45213 45231 45312 45321 51234 51243 51324 51342 51423 51432 52134 52143 52314 52341 52413 52431 53124 53142 53214 53241 53412 53421 随机选取任意多个: 31452 51234 24531 42513 53124 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值