排列组合问题

aaaabbbccd9876 2013-06-06 09:13:44
先举个例子,如abc的组合有a,b,c,,ab,ac,bc,ba,ca,cb,abc,acb,bac,bca,cab,cba.列出所有可能的组合,求实现该功能算法,abc只是个例,是传的一个参数
...全文
153 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
aaaabbbccd9876 2013-06-07
  • 打赏
  • 举报
回复
分不多,请大神们见谅,很感谢你们的帮助
aaaabbbccd9876 2013-06-07
  • 打赏
  • 举报
回复
两位大神果然牛,结贴给分
kosora曹 2013-06-06
  • 打赏
  • 举报
回复
4楼提供的递归算法很好,利用空间树的层次遍历思想,还可以得到非递归算法:


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
Inhibitory 2013-06-06
  • 打赏
  • 举报
回复
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]
地下室森林 2013-06-06
  • 打赏
  • 举报
回复
先按照个数分组,再对每组进行排列
Inhibitory 2013-06-06
  • 打赏
  • 举报
回复
1. 先求组合 2. 对每一个组合求排列
摆烂办不到 2013-06-06
  • 打赏
  • 举报
回复
问题有点复杂 等大神来解

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧