1. Repeatable arrangement: Abc has three characters and all strings of length 3, aaa, aab, aac......ccc has a total of 27 kinds
Using the idea of recursion, the first character can be selected from abc and three choices. Then the problem is transformed into a case where abc consists of characters of length 2. After loop recursion, all possibilities can be found. Just control the loop exit conditions.
Recursion can be used to process it, and when the character length is not known, it is general processing. If you know the length, you only need to use multiple layers of loops to draw conclusions.
public class Permutation { public static void main(String[] args) { char[] chs = {'a','b','c'}; per(new char[3], chs, 3-1); } public static void per(char[] buf, char[] chs, int len){ if(len == -1){ for(int i=buf.length-1; i>=0; --i) System.out.print(buf[i]); System.out.println(); return; } for(int i=0; i<chs.length; i++){ buf[len] = chs[i]; per(buf, chs, len-1); } } }Repeatable selection, a total of 27 situations, the results are shown in the figure below
2. Full arrangement: still three characters abc, full arrangement means that characters cannot be repeated. Last 3*2 = 6 results
You can use the method in 1, and as long as you determine whether the three characters are equal, the one in the full arrangement that is not equal is the one that is needed. Such a time complexity is n^n, and the type of full arrangement is n! So we need to design a kind of n! algorithm.
You can also use recursion. There are n choices for the first string, and the rest becomes a n-1 scale recursion problem. The n choices for the first character are all in the string. Therefore, the first character can be used to exchange it with the position of 1-n to obtain the situation in n, and then recursively process the scale of n-1. However, after processing, it needs to be replaced and become the original character.
public class Arrange { public static void main(String[] args) { char[] chs = {'a','b','c'}; arrange(chs, 0, chs.length); } public static void array(char[] chs, int start, int len){ if(start == len-1){ for(int i=0; i<chs.length; ++i) System.out.print(chs[i]); System.out.println(); return; } for(int i=start; i<len; i++){ char temp = chs[start]; chs[start] = chs[i]; chs[i] = temp; arrangement(chs, start+1, len); temp = chs[start]; chs[start] = chs[i]; chs[i] = temp; } } }The operation results are shown in the figure below, with a total of 6 combinations
3. Combination: All combinations of three characters abc
Find all combinations, that is, whether each bit of abc is selected, the first bit is 2, and the second bit is 2. . . So there are 2^n types in total. 0 means not to take, 1 means selection, so that ab can be represented by 110. The total representation of abc is from 0 to 2^3-1. Then, bitwise operation, if the result is 1, the current bit will be output, and the result 0 will not be output.
public class Comb { public static void main(String[] args) { char[] chs = {'a','b','c'}; comb(chs); } public static void comb(char[] chs) { int len = chs.length; int nbits = 1 << len; for (int i = 0; i < nbits; ++i) { int t; for (int j = 0; j < len; j++) { t = 1 << j; if ((t & i) != 0) { // It will only be 1 when the same as the operation is 1 System.out.print(chs[j]); } } System.out.println(); } } }The output result is as follows. The first behavior is empty, which means that no one is taken.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.