重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
尽量用递归好理解一些,打个断点
专注于为中小企业提供成都网站制作、网站建设、外贸网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业黑龙江免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
public class Permutation {
public static void permulation(int[] list, int start, int length) {
int i;
if (start == length) {
for (i = 0; i length; i++)
System.out.print(list[i] + " ");
System.out.println();
} else {
for (i = start; i length; i++) {
swap(list, start, i);
permulation(list, start + 1, length);
swap(list, start, i);
}
}
}
public static void swap(int[] list, int start, int i) {
int temp;
temp = list;
list = list[i];
list[i] = temp;
}
public static void main(String[] args) {
int length = 3;
int start = 0;
int list[] = new int[length];
for (int j = 0; j length; j++)
list[j] = j + 1;
permulation(list, start, length);
}
}
= =~思路什么的...用递归吧:
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]
递归实现,取数字(字符串)中第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;
}
}