Arrangement "Offer Penalty for prove safety" string

Description Title
input a character string to print all the characters in the string are arranged in lexicographic order. For example input string abc, abc print all strings of characters a, b, c can be arranged out, acb, bac, bca, cab and cba.
Input Description:
enter a string of not more than 9 (possibly repeated characters), characters include only lowercase letters.

Problem-solving ideas:
a string as composed of two parts: The first part of its first character; second part is all of the characters behind it.
(1) The first step in all of the characters in the first character and the following exchange. (Yourself and your exchange is the same, you can not exchange, but the exchange should include the back of the original string)
(2) The second step to the first step of all strings obtained (including the original string) of the second character All characters behind its exchange.
(3) all the characters in the second step a third step of obtaining third character string exchange all subsequent thereto.
(4) Similarly, ....
(5) all the way to the last character.
In short, the complex problem into smaller problems. As above, each time a character is fixed, arranged all subsequent characters seeking. The first step of all character string into the first character and the following, and then fixed to the first character, the characters one by one all the first exchange and the following character. After the exchange we are still behind the removal of the first character of all characters into all of the characters in the first character behind it ... and keep it that way.
Examples shown in the following picture:
Picture Name
the picture of the program sequence is to perform the following sequential code recursion (red arrow).

code show as below:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Solution {
	public ArrayList<String> Permutation(String str) {
		List<String> list = new ArrayList<>();
		if (str.length() == 0)
			return (ArrayList) list;
		Permutation(str.toCharArray(), list, 0);
		Collections.sort(list);
		return (ArrayList) list;
	}

	public void Permutation(char[] charArray, List<String> list, int i) {
		if (i == charArray.length) {// 递归终止条件,结合图片,看图片中方格内箭头的位置,即i的位置,
									// 到达数组末尾时结束递归,即树的叶子结点就是排列的组合
			if (!list.contains(new String(charArray)))// 去掉重复的
				list.add(new String(charArray));
		} else {// 如果未到达数组末尾,则继续递归,把箭头后移,即i+1
			for (int j = i; j != charArray.length; j++) {// 把箭头所在的那一位与后面的每一位交换
				if (j != i) {// 箭头的那一位,没有必要和自己交换,因为交不交换都是一样的
					char t = charArray[j];
					charArray[j] = charArray[i];
					charArray[i] = t;
				}
				Permutation(charArray, list, i + 1);// 继续递归,把箭头后移,即i+1
				if (j != i) {// 递归完返回上一层,要把数组还原,以继续继续下一位的交换
					char t = charArray[j];
					charArray[j] = charArray[i];
					charArray[i] = t;
				}
			}
		}
	}

	public static void main(String[] args) {
		String str = "abc";
		Solution solution = new Solution();
		List<String> list = solution.Permutation(str);
		for (String s : list) {
			System.out.println(s);
		}
	}
}

Guess you like

Origin blog.csdn.net/weixin_44997483/article/details/89875641