您的位置:

全排列算法java描述(全排列算法java描述错误)

本文目录一览:

java 全排列算法;

= =~思路什么的...用递归吧:

package mon_11;

import java.util.HashSet;

public class ArrangeAll {

private static HashSetString set = new HashSetString();

public static void arrangeAll(String s) {

put(new StringBuilder(s), new StringBuilder());

}

static void put(StringBuilder s1, StringBuilder s2) {

if (s1.length() == 0)set.add(s2.toString());

for (int i = 0; i s1.length(); i++) {

put(new StringBuilder(s1).deleteCharAt(i),new StringBuilder(s2).append(s1.charAt(i)));

}

}

public static void main(String[] args) {

arrangeAll("abcd");

System.out.println(set);

}

}

----

输出:

[dcab, acdb, acbd, bcda, bdca, bdac, dbca, bacd, cabd, cdba, cdab, badc, dabc, cadb, dbac, bcad, dacb, cbda, cbad, adbc, adcb, abcd, abdc, dcba]

JAVA 全排列算法

递归实现,取数字(字符串)中第i个位置的字符,然后将他和剩余的字符拼接,剩余的字符串当成有一个全排列的输入,这样递归下去,只剩一个字符时全排列就是本身。程序中使用set去除了重复的数据,如果需要保留,将set换为list接口即可。

package mytest;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Iterator;

import java.util.List;

import java.util.Set;

/*

* @date:2012-2-8

* @author:

*

* 输入一个数字,讲输出 1到这个数字的全排列

*/

public class MyDemo2 {

private static SetString SET_STRING = new HashSetString();

private static SetLong SET_NUM = new HashSetLong();

public static void main(String[] args) {

System.out.println("begin ...... ");

testLong(234);

testString("a23");

print(SET_NUM);

print(SET_STRING);

System.out.println("end ...... ");

}

/**

* 测试数字

* @param num

*/

private static void testLong(long num){

long testNum = num;

String[] permutation;

for(long l=0; l=testNum; l++){

permutation = getAllOrder(String.valueOf(l));

for (int i = 0; i permutation.length; i++) {

SET_NUM.add(Long.valueOf(permutation[i]));

}

}

}

/**

* 测试字符串

* @param str

*/

private static void testString(String str){

String[] permutation = getAllOrder(str);

for (int i = 0; i permutation.length; i++) {

SET_STRING.add(permutation[i]);

}

}

private static void print(Set set){

System.out.println("/*****************************************************/");

int i=0;

for(Iterator it = set.iterator(); it.hasNext();){

i++;

if(i%10 == 0){

System.out.println();

}

System.out.print(it.next() + " ");

}

System.out.println();

System.out.println("/*****************************************************/");

}

/**

* 递归算法 全排列 去除重复

* @param str

* @return

*/

private static String[] getAllOrder(String str) {

String [] arrResult = null;

SetString set = new HashSetString();

if(str.length()1){

String result = "";

String charXInString;

String remainString;

for (int i = 0; i str.length(); i++) {

charXInString = str.charAt(i) + "";

remainString = str.substring(0, i)+ str.substring(i + 1, str.length());

for (String element : getAllOrder(remainString)) {

result = charXInString + element;

set.add(result);

}

}

arrResult = set.toArray(new String[set.size()]);

}else{

arrResult = new String[]{str};

}

return arrResult;

}

}

java全排列 数组

全排列算法很多,这是其中一个,使用递归——

import java.util.ArrayList;

import java.util.List;

public class PermAComb {

    static Listint[] allSorts = new ArrayListint[]();

       

    public static void permutation(int[] nums, int start, int end) {

        if (start == end) { // 当只要求对数组中一个数字进行全排列时,只要就按该数组输出即可

            int[] newNums = new int[nums.length]; // 为新的排列创建一个数组容器

            for (int i=0; i=end; i++) {

                newNums[i] = nums[i];

            }

            allSorts.add(newNums); // 将新的排列组合存放起来

        } else {

            for (int i=start; i=end; i++) {

                int temp = nums; // 交换数组第一个元素与后续的元素

                nums = nums[i];

                nums[i] = temp;

                permutation(nums, start + 1, end); // 后续元素递归全排列

                nums[i] = nums; // 将交换后的数组还原

                nums = temp;

            }

        }

    }

       

    public static void main(String[] args) {

        int[] numArray = {1, 2, 3, 4, 5, 6};

        permutation(numArray, 0, numArray.length - 1);

        int[][] a = new int[allSorts.size()][]; // 你要的二维数组a

        allSorts.toArray(a);

           

        // 打印验证

        for (int i=0; ia.length; i++) {

            int[] nums = a[i];

            for (int j=0; jnums.length; j++) {

                System.out.print(nums[j]);

            }

            System.out.println();

        }

        System.out.println(a.length);

    }

}