Cracking the coding interview--Q8.4

原文:

Write a method to compute all permutations of a string

译文:

写一个函数返回一个串的所有排列


比如,计算 “abc”的全排列

abc acb bac bca cab cba


对于一个长度为n的串,它的全排列共有A(n, n)=n!种。这个问题也是一个递归的问题, 不过可以用不同的思路去理解它。为了方便讲解,假设要考察的串是"abc", 递归函数名叫getAllOrder。


思路一:

我们可以把串“abc”中的第0个字符a取出来,然后递归调用permu计算剩余的串“bc” 的排列,得到{bc, cb}。然后再将字符a插入这两个串中的任何一个空位(插空法), 得到最终所有的排列。比如,a插入串bc的所有(3个)空位,得到{abc,bac,bca}。 递归的终止条件是什么呢?当一个串为空,就无法再取出其中的第0个字符了, 所以此时返回一个空的排列。代码如下:

	/**
	 * @param str
	 * @param len
	 * @param curIndex
	 * @return
	 * 
	 * 把串“abc”中的第0个字符a取出来,然后递归调用getAllOrder计算剩余的串“bc” 的排列,得到{bc, cb}。
	 * 然后再将字符a插入这两个串中的任何一个空位(插空法), 得到最终所有的排列。
	 * 
	 */
	public static LinkedList<LinkedList<Character>> getAllOrder(String str, int len, int curIndex) {
		LinkedList<LinkedList<Character>> result = new LinkedList<LinkedList<Character>>();
		if(curIndex == len) {
			LinkedList<Character> sub = new LinkedList<Character>();
			result.add(sub);
		} else {
			LinkedList<LinkedList<Character>> part = getAllOrder(str, len, curIndex +1);
			int desk = len - curIndex;
			for(LinkedList<Character> temp: part) {
				for(int i=0; i< desk; i++) {
					
					LinkedList<Character> newOrder = new LinkedList<Character>();
					Iterator<Character> it = temp.iterator();
					
					for(int j=0; j<i ;j++) {
						newOrder.add(it.next());
					}
					newOrder.add(str.charAt(curIndex));
					while(it.hasNext()) {
						newOrder.add(it.next());
					}
					
					result.add(newOrder);
				}
			}
		}
		return result;
	}


思路二:

我们还可以用另一种思路来递归解这个问题。还是针对串“abc”, 我依次取出这个串中的每个字符,然后调用permu去计算剩余串的排列。 然后只需要把取出的字符加到剩余串排列的每个字符前即可。对于这个例子, 程序先取出a,然后计算剩余串的排列得到{bc,cb},然后把a加到它们的前面,得到 {abc,acb};接着取出b,计算剩余串的排列得到{ac,ca},然后把b加到它们前面, 得到{bac,bca};后面的同理。最后就可以得到“abc”的全序列。代码如下:


	/**
	 * @param str
	 * @return
	 * 
	 * 针对串“abc”, 每次取出一个元素,计算剩余元素的全排列,把取出元素放在首位。依次递归求解。
	 * 递归出口是剩余元素为空,建立空链表
	 * 
	 */
	public static LinkedList<LinkedList<Character>> getAllOrder2(String str) {
		LinkedList<LinkedList<Character>> result = new LinkedList<LinkedList<Character>>();
		if(str.length() == 0) {
			LinkedList<Character> part = new LinkedList<Character>();
			result.add(part);
		} else {
			for(int i=0; i< str.length(); i++) {
				char ch = str.charAt(i);
				String substring =  (i < str.length() -1) ? (str.substring(0, i) + str.substring(i+1)):(str.substring(0,i));

				LinkedList<LinkedList<Character>> part = getAllOrder2(substring);
				for(LinkedList<Character> temp: part) {
					LinkedList<Character> newPart = new LinkedList<Character>();
					newPart.add(ch);
					Iterator<Character> it = temp.iterator();
					while(it.hasNext()) {
						newPart.add(it.next());
					}
					
					result.add(newPart);
				}
			}
		}
		return result;
	}


全部代码如下:

package chapter_8_Recursion;

import java.util.Iterator;
import java.util.LinkedList;

/**
 *
 * 写一个函数返回一个串的所有排列
 * 
 */
public class Question_8_4 {
	
	/**
	 * @param str
	 * @param len
	 * @param curIndex
	 * @return
	 * 
	 * 把串“abc”中的第0个字符a取出来,然后递归调用getAllOrder计算剩余的串“bc” 的排列,得到{bc, cb}。
	 * 然后再将字符a插入这两个串中的任何一个空位(插空法), 得到最终所有的排列。
	 * 
	 */
	public static LinkedList<LinkedList<Character>> getAllOrder(String str, int len, int curIndex) {
		LinkedList<LinkedList<Character>> result = new LinkedList<LinkedList<Character>>();
		if(curIndex == len) {
			LinkedList<Character> sub = new LinkedList<Character>();
			result.add(sub);
		} else {
			LinkedList<LinkedList<Character>> part = getAllOrder(str, len, curIndex +1);
			int desk = len - curIndex;
			for(LinkedList<Character> temp: part) {
				for(int i=0; i< desk; i++) {
					
					LinkedList<Character> newOrder = new LinkedList<Character>();
					Iterator<Character> it = temp.iterator();
					
					for(int j=0; j<i ;j++) {
						newOrder.add(it.next());
					}
					newOrder.add(str.charAt(curIndex));
					while(it.hasNext()) {
						newOrder.add(it.next());
					}
					
					result.add(newOrder);
				}
			}
		}
		return result;
	}
	
	/**
	 * @param str
	 * @return
	 * 
	 * 针对串“abc”, 每次取出一个元素,计算剩余元素的全排列,把取出元素放在首位。依次递归求解。
	 * 递归出口是剩余元素为空,建立空链表
	 * 
	 */
	public static LinkedList<LinkedList<Character>> getAllOrder2(String str) {
		LinkedList<LinkedList<Character>> result = new LinkedList<LinkedList<Character>>();
		if(str.length() == 0) {
			LinkedList<Character> part = new LinkedList<Character>();
			result.add(part);
		} else {
			for(int i=0; i< str.length(); i++) {
				char ch = str.charAt(i);
				String substring =  (i < str.length() -1) ? (str.substring(0, i) + str.substring(i+1)):(str.substring(0,i));

				LinkedList<LinkedList<Character>> part = getAllOrder2(substring);
				for(LinkedList<Character> temp: part) {
					LinkedList<Character> newPart = new LinkedList<Character>();
					newPart.add(ch);
					Iterator<Character> it = temp.iterator();
					while(it.hasNext()) {
						newPart.add(it.next());
					}
					
					result.add(newPart);
				}
			}
		}
		return result;
	}
	
	public static void printAllOrder(LinkedList<LinkedList<Character>> result) {
		Iterator<LinkedList<Character>> it = result.iterator();
		while(it.hasNext()) {
			LinkedList<Character> child = it.next();
			Iterator<Character> itChild = child.iterator();
			while(itChild.hasNext()) {
				System.out.print(itChild.next());
			}
			System.out.println();
		}
	}
	
	public static void main(String args[]) {
		String str = "abcd";
		
		LinkedList<LinkedList<Character>> result = getAllOrder(str, str.length(), 0);
		
		printAllOrder(result);
		
		System.out.println("-------------");
		
		String str2 = "acfh";
		LinkedList<LinkedList<Character>> result2 = getAllOrder2(str2);
		printAllOrder(result2);
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值