java实现给定字符串之间的全排列算法

最近看了《编程之法:面试和算法心得》这本书,被各种高深的算法给膜拜了(本人职场小白,开发经验有限,抱着一颗学习的心,向大神学习),在这里整理了书中的部分算法,主要有两个目的:

1、做记录,为了以后使用方便查询。

2、分享学习,发表博客分享好东西,大家相互学习,有什么错误的地方,也请各位大神指教!


本文章是以《编程之法:面试和算法心得》书籍作为基础,将书籍中的算法使用java语言进行整理,以及个人所列举的计算方法,本文章根据本书中的第一章第三节字符串的全排列进行整理,并且提出了本人平时使用的计算方法(本人方法卓略,大神勿喷),以供日后查阅,及分享!

以下为个人根据书籍整理的算法,以java语言编程,代码如下:

书籍中所提供的算法,在这里只整理出第一种解法,第二种解法一直没看明白,在这里将书籍的第二种解法贴出,希望有大神能够留言讲解一下。

希望有大神能不惜赐教!小弟不胜感激!


以下为个人根据书籍整理的第一种算法,以及一个外国网站的开发大神提供的算法,以java语言编程,代码如下:

package bianchengzhifaExample;

/**
 * 全排列
 * @author cyz
 *
 */
public class CalcAllPermutation {
    
	
	/**
     * 递归实现全排列  
     * @param str 全排列字符串
     * @param st  全排列开始位置
     * @param len 全排列结束位置
     */
    public String calcAllPermutation1(String[] str, int st, int len)  
    {   String s=new String();
        if (st == len - 1)  
        {   
            for (int i = 0; i < len; i ++)  
            {  
            	s+=str[i];
            }  
            s+="  ";
        }  
        else  
        {  
            for (int i = st; i < len; i ++)  
            {  
                swap(str, st, i);  
                s+=calcAllPermutation1(str, st + 1, len);  
                swap(str, st, i);  
            }  
        }  
        return s;
    }  
    public void swap(String[] str, int i, int j)  
    {  
        String temp=new String() ;  
        temp = str[i];  
        str[i] = str[j];  
        str[j] = temp;  
    }
    
    /**
     * 使用笛卡尔积进行全排序
     */
    private static String[] aa = { "aa1", "aa2" };  
    private static String[] bb = { "bb1", "bb2", "bb3" };  
    private static String[] cc = { "cc1", "cc2", "cc3", "cc4" };  
    private static String[][] xyz = { aa, bb, cc };  
    private static int counterIndex = xyz.length - 1;  
    private static int[] counter = { 0, 0, 0 };  
  
    public static void calcAllPermutation2() {  
        counter[counterIndex]++;  
        if (counter[counterIndex] >= xyz[counterIndex].length) {  
            counter[counterIndex] = 0;  
            counterIndex--;  
            if (counterIndex >= 0) {  
            	calcAllPermutation2();  
            }  
            counterIndex = xyz.length - 1;  
        }  
    }  
    
    
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
    	CalcAllPermutation cap=new CalcAllPermutation();
    	String str[] = {"a","b","c"};  
    	System.err.println("---------使用递归实现全排列---------------");
    	String s=cap.calcAllPermutation1(str, 0, str.length); 
    	System.err.println("s:"+s);
    	System.err.println("---------使用笛卡尔积实现全排列---------------");
    	for (int i = 0; i < aa.length * bb.length * cc.length; i++) {  
    	    System.err.print(aa[counter[0]]);  
    	    System.err.print("\t");  
    	    System.err.print(bb[counter[1]]);  
    	    System.err.print("\t");  
    	    System.err.print(cc[counter[2]]);  
    	    System.err.println();  
    	    calcAllPermutation2();  
    	 }  
    }  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值