按字典顺序打印字符串的所有排列
2017-04-25 17:12
316 查看
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
import java.util.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> result = new ArrayList<String>() ; if(str==null || str.length()==0) { return result ; } char[] chars = str.toCharArray() ; TreeSet<String> temp = new TreeSet<>() ; Permutation(chars, 0, temp); result.addAll(temp) ; return result ; } public void Permutation(char[] chars, int begin, TreeSet<String> result) { if(chars==null || chars.length==0 || begin<0 || begin>chars.length-1) { return ; } if(begin == chars.length-1) { result.add(String.valueOf(chars)) ; }else { for(int i=begin ; i<=chars.length-1 ; i++) { swap(chars, begin, i) ; Permutation(chars, begin+1, result); swap(chars, begin, i) ; } } } public void swap(char[] x, int a, int b) { char t = x[a]; x[a] = x[b]; x[b] = t; } }
相关文章推荐
- 打印输入字符串的字母所有顺序的排列
- 编写程序,循环接收用户从键盘输入多个字符串,直到输入“ end”时循环结束,并将所有 已输入的字符串按字典顺序倒序打印。
- 编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
- 输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 结果请按字母顺序输出。
- 编写程序,循环接收用户从键盘输入多个字符串,直到输入“end”时循环结束,并将所有已输入的字符串按字典顺序倒序打印。
- 输入一个字符串,打印出该字符串中字符的所有排列。
- 用字典顺序得到所有的排列
- 打印给定字符串中字符的所有排列
- 打印出字符串中字符的所有排列
- 输入一个字符串,打印出该字符串中字符的所有排列
- 使用循环的方式打印字符串所有排列组合
- 输入一个字符串,打印出该字符串中字符的所有排列
- 一个打印输入字符串所有排列的递归程序
- 列出所有排列(字典顺序)
- 编写一个程序,输入一串不超过60个字符组成的且不包含换行符的字符串,将字符串中的A到Z的字母按英文字典字母顺序重新排列,排列后的单词的长度要与原始句子中的长度相同,并且要求只对A到Z的字母重新排列,其
- 输入一个字符串,打印出该字符串中字符的所有排列
- 按递增顺序打印数字n所有排列
- 产生所有排列---字典顺序-----2013年1月23日
- 字符串全排列:输入一个字符串,打印字符串元素的所有排列
- C语言实现输入一个字符串后打印出该字符串中字符的所有排列