字符串的排列

来源:互联网 发布:域名属于知识产权吗 编辑:程序博客网 时间:2024/06/04 22:22

输入一个字符数组,打印出该字符数组中字符的所有不重复的排列。

import java.util.ArrayList;import java.util.List;public class Main {static List<String> lstchs = new ArrayList<String>();public static void main(String[] args) {// TODO Auto-generated method stubchar[] chs = {'F','G','J','J','H','K','S','L'};lstchs.clear();Permutation(chs);for(int i = 0; i<lstchs.size();i++) {System.out.println(lstchs.get(i).toString());}}public static void Permutation(char[] chs) {if(chs.length == 0)return ;Permutation(chs,0,0,chs.length);}public static void Permutation(char[] chs,int pBegin,int pstr,int len) {if(pBegin == len) {String s = "";int i = 0;while(i<len) {s += chs[i];i += 1;}if(lstchs.contains(s) == false)lstchs.add(s);}else {for(int index=pBegin;index<len;index++) {char temp = chs[index];chs[index] = chs[pBegin];chs[pBegin] = temp;Permutation(chs,pBegin+1,pstr,len);temp = chs[index];chs[index] = chs[pBegin];chs[pBegin] = temp;}}}}