博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
符串的排列
阅读量:4624 次
发布时间:2019-06-09

本文共 1061 字,大约阅读时间需要 3 分钟。

输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。 
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
 
1 import java.util.*; 2 public class Solution { 3     public ArrayList
Permutation(String str) { 4 ArrayList
re = new ArrayList
(); 5 if (str == null || str.length() == 0) { 6 return re; 7 } 8 HashSet
set = new HashSet
(); 9 fun(set, str.toCharArray(), 0);10 re.addAll(set);11 Collections.sort(re);12 return re;13 }14 void fun(HashSet
re, char[] str, int k) {15 if (k == str.length) {16 re.add(new String(str));17 return;18 }19 for (int i = k; i < str.length; i++) {20 swap(str, i, k);21 fun(re, str, k + 1);22 swap(str, i, k);23 }24 }25 void swap(char[] str, int i, int j) {26 if (i != j) {27 char t = str[i];28 str[i] = str[j];29 str[j] = t;30 }31 }32 }

 

 
 

转载于:https://www.cnblogs.com/LoganChen/p/6474594.html

你可能感兴趣的文章
maven阿里云中央仓库
查看>>
一:SpringMVC架构流程
查看>>
Dubbo的架构
查看>>
HDU5108
查看>>
HTML5在VS2010中的智能提示
查看>>
BZOJ2735 : 世博会
查看>>
P2258 子矩阵(dp)
查看>>
密度峰值聚类算法——心得总结
查看>>
div中字符串自动换行
查看>>
DevExpress v17.2新版亮点—WPF篇(四)
查看>>
Kendo UI使用教程:CDN服务
查看>>
浮躁的过去,开启的项目管理之路(三)
查看>>
Codeforces441C_Valera and Tubes(暴力)
查看>>
二层交换机原理
查看>>
关于编码和字库
查看>>
shell 脚本启动 flask
查看>>
mysql触发器
查看>>
iis设置局域网访问,Context.Request.Url.Authority老是取出为localhost问题
查看>>
并发编程 16—— 线程池 之 原理二
查看>>
Python学习笔记-DAY-4
查看>>