62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.LinkedList;
import java.util.Queue;
import org.junit.Test;
/**
* 幂集的节点
*
*/
class MijiNode{
/**
* 数据
*/
private String data;
/**
* 层
*/
private int ceng;
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public int getCeng() {
return ceng;
}
public void setCeng(int ceng) {
this.ceng = ceng;
}
public MijiNode(String data, int ceng) {
super();
this.data = data;
this.ceng = ceng;
}
public MijiNode() {
super();
}
@Override
public String toString() {
return "MijiNode [data=" + data + ", ceng=" + ceng + "]";
}
}
/**
* 全排列节点
*
*/
class AllSortNode{
/**
* 左子串
*/
private String leftStr;
/**
* 右子串
*/
private String rightStr;
public String getLeftStr() {
return leftStr;
}
public void setLeftStr(String leftStr) {
this.leftStr = leftStr;
}
public String getRightStr() {
return rightStr;
}
public void setRightStr(String rightStr) {
this.rightStr = rightStr;
}
public AllSortNode(String leftStr, String rightStr) {
super();
this.leftStr = leftStr;
this.rightStr = rightStr;
}
public AllSortNode() {
super();
}
@Override
public String toString() {
return "AllSortNode [leftStr=" + leftStr + ", rightStr=" + rightStr
+ "]";
}
}
public class MijiAndAllSort {
/**
*求幂集
*/
public Queue<MijiNode> createMiji(String str){
Queue<MijiNode> que=new LinkedList<MijiNode>();
MijiNode root=new MijiNode("", 0);
que.offer(root);
while(que.size()<Math.pow(2, str.length())){
MijiNode p=que.poll();
MijiNode leftCh=new MijiNode(p.getData()+str.charAt(p.getCeng()), p.getCeng()+1);
MijiNode rightCh=new MijiNode(p.getData(), p.getCeng()+1);
que.offer(leftCh);
que.offer(rightCh);
}
for(MijiNode node:que){
String s=node.getData();
StringBuffer sb=new StringBuffer();
int i=0;
for(;i<s.length();i++){
sb.append(s.charAt(i));
}
node.setData(sb.toString());
}
return que;
}
/**
* 求全排列
*/
public void allSort(String s){
Queue<AllSortNode> que=new LinkedList<AllSortNode>();
String leftRoot="";
String rightRoot=s;
AllSortNode root=new AllSortNode(leftRoot, rightRoot);
que.offer(root);
while(true){
if(que.peek().getRightStr().length()==0){
break;
}else{
AllSortNode currentRoot=que.poll();
String rightStr=currentRoot.getRightStr();
String leftStr=currentRoot.getLeftStr();
int len=rightStr.length();
for(int i=0;i<len;i++){
StringBuffer sb=new StringBuffer(rightStr);
sb.deleteCharAt(i);
String leftChStr=leftStr+" "+rightStr.charAt(i);
String rightChStr=sb.toString();
AllSortNode node=new AllSortNode(leftChStr, rightChStr);
que.offer(node);
}
}
}
while(!que.isEmpty()){
System.out.println(que.poll().getLeftStr());
}
}
@Test
public void testMijiAndAllSort(){
Queue<MijiNode> que=this.createMiji("abc");
String s=null;
for(MijiNode node:que){
s=node.getData();
if(s!=null){
this.allSort(s);
}
}
}
}
结果:
a b c
a c b
b a c
b c a
c a b
c b a
a b
b a
a c
c a
a
b c
c b
b
c
import java.util.LinkedList;
import java.util.List;
public class Hello {
/**
* 先求组合,对每个组合求排列.
*/
private static List<Character> combinationResult = new LinkedList<Character>();
public static void combination(Character[] chs, int index) {
for (int i = index; i < chs.length; ++i) {
combinationResult.add(chs[i]);
permutation(combinationResult.toArray(new Character[0]), 0);
combination(chs, i + 1);
combinationResult.remove(combinationResult.size() - 1);
}
}
/**
* 求排列.
*/
private static List<Character> permutationResult = new LinkedList<Character>();
public static void permutation(Character[] chs, int index) {
for (int i = 0; index < chs.length && i < chs.length; ++i) {
if (permutationResult.contains(chs[i])) {
continue;
}
permutationResult.add(chs[i]);
if (permutationResult.size() == chs.length) {
System.out.println(permutationResult);
}
permutation(chs, index + 1);
permutationResult.remove(permutationResult.size() - 1);
}
}
public static void main(String[] args) {
String str = "abc";
List<Character> chs = new LinkedList<Character>();
for (char c : str.toCharArray()) {
chs.add(c);
}
combination(chs.toArray(new Character[0]), 0);
}
}
[a]
[a, b]
[b, a]
[a, b, c]
[a, c, b]
[b, a, c]
[b, c, a]
[c, a, b]
[c, b, a]
[a, c]
[c, a]
[b]
[b, c]
[c, b]
[c]