一個比較優化的算法
- import java.util.Set;
- import java.util.TreeSet
- /**
- * 原始版本沒有考慮重複問題,我進行了修正.<br>
- * 此算法的精華就是,讓一個字符串的所有位置都進行互換,<br>
- * 這樣就產生了所有可能出現的字符串。
- *
- * @author 趙學慶,nicky_zs <A href="http://www.java2000.net">www.java2000.net</A>
- */
- public class MyTest {
- public static void main(String args[]) {
- char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
- perm(number, 0, number.length - 1);
- System.out.println(set.size());
- int cols = 10;
- for (String s : set) {
- System.out.print(s + " ");
- if (cols-- == 1) {
- System.out.println();
- cols = 10;
- }
- }
- }
- public static void addNumber(String str) {
- set.add(str);
- }
- public static Set<String> set = new TreeSet<String>();
- public static void perm(char[] n, int beg, int end) {
- if (beg == end) {
- String result = String.valueOf(n);
- if (n[2] == '4')
- return;
- if (result.contains("35") || result.contains("53"))
- return;
- addNumber(String.valueOf(n));
- return;
- }
- for (int i = beg; i <= end; ++i) {
- swap(n, beg, i);
- perm(n, beg + 1, end);
- swap(n, beg, i);
- }
- }
- public static int number = 0;
- public static void swap(char[] n, int a, int b) {
- char temp = n[a];
- n[a] = n[b];
- n[b] = temp;
- }
- }