字符串的全排列
来源:互联网 发布:数据产品经理发展前景 编辑:程序博客网 时间:2024/05/16 14:06
题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。
思路1:
假设字符串长度为n,当n为1时则直接返回其本身,否则首先计算后n-1个字符的所有排列(共(n-1)!个字符串),针对每个字符串将第一个字符插入到该字符串的相应位置中(有n种位置),例如将a插入到字符串bc中可以得到abc、bac、bca,所以最终就可以得到(n-1)!*n个结果字符串即我们需要的n!个字符串。
思路2:
借助回溯的思想递归:
以abc为例来分析一下求字符串全排列的过程。首先我们固定第一个字符a,求后面两个字符bc的排列。当两个字符bc的排列求好之后,我们把第一个字符a和后面的b交换,得到bac,接着我们固定第一个字符b,求后面两个字符ac的排列,现在是把c放到第一位置的时候了。记住前面我们已经把原先的第一个字符a和后面的b做了交换,为了保证这次c仍然是和原先处在第一位置的a交换,我们在拿c和第一个字符交换之前,先要把b和a交换回来。在交换b和a之后,再拿c和处在第一位置的a进行交换,得到cba。我们再次固定第一个字符c,求后面两个字符b、a的排列。既然我们已经知道怎么求三个字符的排列,那么固定第一个字符之后求后面两个字符的排列,就是典型的递归思路了。
import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;public class PermutationOfString { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); System.out.println("input the size of String:"); int n = sc.nextInt(); System.out.println("input the String:"); String s = sc.next(); ArrayList<String> list = Permutation1(s); System.out.println("the results:"); for(int i=0;i<list.size();i++){ System.out.print(list.get(i) + " "); } } //思路1 public static ArrayList<String> Permutation(String str) { ArrayList<String> stringList = new ArrayList<String>(); if (str == null || str.equals("")) { return stringList; } if (str.length() == 1) { stringList.add(str); } else { char firstChar = str.charAt(0); ArrayList<String> tempList = Permutation(str.substring(1)); for (int i = 0; i < tempList.size(); i++) { String temp = tempList.get(i); for (int j = 0; j <= temp.length(); j++) { String firstHalf = temp.substring(0, j); String tailHalf = temp.substring(j); String result = firstHalf + firstChar + tailHalf; if(!stringList.contains(result)){ stringList.add(firstHalf + firstChar + tailHalf); } } } } Collections.sort(stringList);//按照自然顺序排列 return stringList; } //思路2 public static ArrayList<String> Permutation1(String str){ ArrayList<String> stringList = new ArrayList<String>(); if (str == null || str.equals("")) { return stringList; } allRank(stringList, str.toCharArray(), 0); Collections.sort(stringList); return stringList; } public static void allRank(ArrayList<String> stringList, char[] str, int k){ if(k == str.length){ //去除重复排列 if(!stringList.contains(new String(str))){ stringList.add(new String(str)); return; } } for(int i=k;i<str.length;i++){ swap(str,i,k); allRank(stringList,str, k+1); swap(str,i,k); } } public static void swap(char[] str, int i, int j) { if(i != j){ char t = str[i]; str[i] = str[j]; str[j] = t; } }}
0 0
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 字符串的全排列
- 软考程序员计算机专业英语
- Codeforces 673D Bear and Two Paths (贪心构造)
- NAT 苹果要求支持ipv6的原因吧。
- c++小游戏之贪吃蛇
- linux进程调度之 FIFO 和 RR 调度策略
- 字符串的全排列
- 提高篇项目4——求阶乘函数(1)
- ISA TEST黑客过关小游戏第二关解密
- 数据库索引的实现原理
- 数字三角形
- 矩阵乘法+快速幂优化递推式
- Java 设计模式(十一) 里氏替换原则(LSP)
- UDP协议
- 开发板Linux系统制作