递归实现,取数字(字符串)中第i个位置的字符,然后将他和剩余的字符拼接,剩余的字符串当成有一个全排列的输入,这样递归下去,只剩一个字符时全排列就是本身。程序中使用set去除了重复的数据,如果需要保留,将set换为list接口即可。
十载的徐州网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整徐州建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“徐州网站设计”,“徐州网站推广”以来,每个客户项目都认真落实执行。
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;
}
}
= =~思路什么的...用递归吧:
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]
对 深度优先搜索 先交换 然后递归 再交换回来继续循环下一种情况
尽量用递归好理解一些,打个断点
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);
}
}