Variety of writing full array

From any of n different elements taken m (m≤n) elements, lined up in a certain order, called a permutation of m elements taken from n different elements. When m = n all the arrangement of the whole arrangement is called. Solution The following are the full character string is arranged in a column.

Gradually resulting solution:

This solution is characterized by: a full permutation of elements from slowly introduced full permutation of n elements. A full array element Release full array of two elements, the whole arrangement is then re-introduce the two elements of the full array of three elements. Release until the full array of n elements.

In figures 2, 3, for example, a tree structure can be represented using full permutation generation algorithm, a digital to full permutation generation algorithm, for example, start from the smallest number 1, which is only one possible arrangement of the whole; added the number 2, numeral 2 can be inserted in the back or front 1, there are two different positions; was added 3, a different arrangement for each of the second layer 3 may be inserted into different positions by three different numbers are arranged to give a total of 6 kinds of the number of permutations; analogy can be obtained a full array of three numbers.
Here Insert Picture Description

Iteration worded as follows:

import java.util.*;

public class 全排列迭代写法 {
	public static void main(String[] args){
		String str = "abc";
		System.out.println(perm(str));
	}

	private static Set<String> perm(String str) {
		Set<String> resset = new HashSet();
		resset.add(str.charAt(0)+ "");
		for(int i=1; i<str.length(); i++){//每一次循环产生i个字母的所有全排列集合
			Set<String> set = new HashSet();
			for(String s:resset){
				char c = str.charAt(i);
				//必须将加在最前面和加在最后面从循环中单独拿出来
				//否则会影响循环的次数
				set.add(c + s);//添加在最前面
				set.add(s+c);//添加在最后面
				for(int j=1; j<s.length(); j++){//在中间插入新字符
					String t = s.substring(0,j) + c + s.substring(j);
					set.add(t);
				}
			}
			resset = set;//新的一层集合覆盖原来的集合
		}
		return  resset;
	}
}

Recursive worded as follows:

import java.util.*;

public class 全排列递归写法 {
	public static void main(String[] args){
		String str = "abc";
		System.out.println(perm(str,str.length(), str.length()-1));
	}

	private static Set<String> perm(String str, int n, int cur) {
		Set<String> newset = new HashSet(); 
		if(cur==0){
			newset.add(str.charAt(0)+"");
			return newset;
		}
		Set<String> set = perm(str, n-1, cur-1);
		for(String s:set){
			char c = str.charAt(cur);
			newset.add(c + s);//添加在最前面
			newset.add(s+c);//添加在最后面
			for(int j=1; j<s.length(); j++){//在中间插入新字符
				String t = s.substring(0,j) + c + s.substring(j);
				newset.add(t);
			}
		}
		return newset;
	}
}

Backtracking algorithm:

Backtracking recursive solution is characterized by a multi-branch, for example letters: first find the beginning of the arrangement A, and then find the beginning of the arrangement B, and finally find out the beginning of the arrangement C. It is the spanning tree generation process after the first longitudinal horizontal. More down branches will continue to decrease, to one leg when he walked in the end get a full array; and then backtracking. A large branch to the beginning of this post need to return to finish the walk at the beginning of the root node B branch.
Here Insert Picture Description
code show as below:

import java.util.*;

public class 全排列回溯写法 {
	public static void main(String[] args){
		String str = "abc";
		char[] a = str.toCharArray();
		ArrayList<String> res = new ArrayList();
		perm(a, 0, res);
		System.out.println(res);
	}
	/**
	 * @param a
	 * @param n
	 * @param res
	 * 将数组下标k以及之后的元素全排列的结果添加进res中
	 */
	private static void perm(char[] a, int k, ArrayList res) {
		if(k==a.length){//得到了一种全排列,生成树的其中一条支路走到底了
			String str = new String(a);
			res.add(str);
		}
		//从k位开始的每一个字符都尝试放在新排列的k这个位置
		for(int i=k; i<a.length; i++){
			swap(a, k, i);//将后面的字母换到k位
			perm(a, k+1, res);//将k-1后的元素进行全排列
			swap(a, k, i);//回溯
		}
		
	}
	private static void swap(char[] a, int k, int i) {
		char temp = a[k];
		a[k] = a[i];
		a[i] = temp;
	}

}

This code need to be addressed if there is duplication of the ArrayList changed Set on the line.

Published 45 original articles · won praise 5 · Views 1929

Guess you like

Origin blog.csdn.net/HC199854/article/details/104473601