网站建设资讯

NEWS

网站建设资讯

求全排列java代码,java排序代码完整

一个JAVA编程求助:用递归方法求n(n=3)个数的无重复全排列

import java.util.ArrayList;

成都创新互联公司是一家专注于网站设计制作、成都网站设计与策划设计,广阳网站建设哪家好?成都创新互联公司做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:广阳等地区。广阳做网站价格咨询:18982081108

import java.util.List;

public class Test {

public static void main(String[] args) {

Test t = new Test();

t.contList();

t.getAllArray(list, 0);

}

private static ListInteger list = new ArrayList();

private void contList(){

list.add(1);

list.add(2);

list.add(3);

}

public void getAllArray(ListInteger inlist,int site){

int tempsite = site;

if(site = inlist.size()){

return;

}

Integer firstNode = inlist.get(site++);

List tempList = new ArrayList(inlist);

tempList.remove(tempsite);

for(int i = 0;i tempList.size();i++){

System.out.print(firstNode);

for(int j = i;j tempList.size()+i;j++){

if(j tempList.size()){

System.out.print(","+tempList.get(j));

}else{

System.out.print(","+tempList.get(j-tempList.size()));

}

}

System.out.println("");

}

getAllArray(inlist,site);

}

}

n=3这个运行结果是

1,2,3

1,3,2

2,1,3

2,3,1

3,1,2

3,2,1

import java.util.*;

import java.io.*;

public class Test {

public static void main(String[] args) throws Exception {

String[] array = new String[] { "1", "2", "3", "4" };

int length=array.length;

listAll(Arrays.asList(array), "",length);

}

public static void listAll(List candidate, String prefix,int length) {

if(prefix.length()==length)

System.out.println(prefix);

for (int i = 0; i candidate.size(); i++) {

List temp = new LinkedList(candidate);

listAll(temp, prefix + temp.remove(i),length);

}

}

}

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怎么搞全排列

尽量用递归好理解一些,打个断点

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);

}

}

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全排列递归算法

思路:先有一个起始排列,如1234.从后面扫描,直到找到a[k],a[k]a[k+1];再从后面扫描,直到找到a[j],这里有 a[k]a[j]。交换a[k],a[j].再把a[k+1],...a[n-1]排序(从小到大),即得到了一个排列,再循环下去,直到找出所有的排序。用C语言的,参考下:


网页标题:求全排列java代码,java排序代码完整
分享URL:http://cdweb.net/article/pheodc.html