[CC150] Get all permutations of a string
Problem: Compute all permutations of a string of unique characters.
此题用循环的方式不好做,下面是一种递归的思路:

把给的字符串看成一个字符集合,每次从这个集合里拿出一个字符来填到下面的空格中。填的顺序从左到右。
把a1填到第一个空格里是一种情况,集合剩下的部分填到2-9的空格里;
把a2填到第一个空格里是一种情况,集合剩下的部分填到2-9的空格里;
。。。。。。
这样递归的进行下去,直到集合里没有剩余,所有的情况就被穷尽了。
public ArrayList<String> getPerms(String str){
ArrayList<String> result = new ArrayList<String>();
getPerms("", str, result);
return result;
}
public void getPerms(String prefix, String str, ArrayList<String> result){
if(str == null || str.length() == 0){
result.add(prefix);
return;
}
for(int i = 0; i < str.length(); ++i){
char ch = str.charAt(i);
getPerms(prefix+ch, str.substring(0, i) + str.substring(i+1), result);
}
}
[CC150] Get all permutations of a string的更多相关文章
- 9.5---所有字符串的排列组合(CC150)
1,这个是自己写的.一直LTE. public static ArrayList<String> getPerms(String str) { if (str == null) { ret ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- 不会全排列算法(Javascript实现),我教你呀!
今天我很郁闷,在实验室凑合睡了一晚,准备白天大干一场,结果一整天就只做出了一道算法题.看来还是经验不足呀,同志仍需努力呀. 算法题目要求是这样的: Return the number of total ...
- fcc的高级算法题
核心提示:本部分一个9道题,给定时间50小时.属于fcc前端学习的"高级编程脚本"题,对于初学者来说,确实算是"高级"了.如果只想着闭门造车,50小时确实也不过 ...
- 字符串全排列(permutation)
Reference: http://www.cnblogs.com/sujz/archive/2011/06/16/2082831.html 问题:给定字符串S,生成该字符串的全排列. 方法1:依次从 ...
- HDOJ-ACM1015(JAVA) 运用 组合、全排列实现
转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5573934.html 这个题目的题意:(自己结合百度翻译,简单的翻译了一下) “这个项目是在一个在二楼图书馆一幅 ...
- CCI_chapter 8 Recurision
8.1 水题 8.2 Imagine a robot sitting on the upper left hand corner of an NxN grid The robot can only m ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目5
2014-03-20 03:23 题目:给定一个字符串,输出其全排列. 解法:可以调用STL提供的next_permutation(),也可以自己写一个.对于这种看起来简单的题目,应该在能优化的地方, ...
随机推荐
- 常用CentOS 6/7 扩展源
1.系统自带 baseextrasupdates 2.epel yum install epel-release https://fedoraproject.org/wiki/EPEL 3.el(用于 ...
- 使用Zencoding的升级版Emmet
最近vim7.4也更新了,zengcoding好像停止了更新,更换成emmet了,使用还是和Zencoding一样. emmet的地址在http://emmet.io/ 支持很多的编辑器和IDE. ...
- 回环栅栏CyclicBarrier
通过它可以实现让一组线程等待至某个状态之后再全部同时执行.叫做回环是因为当所有等待线程都被释放以后,CyclicBarrier可以被重用.我们暂且把这个状态就叫做barrier,当调用await()方 ...
- B/S的验证控件
验证控件 首先设置一下框架,设置为.net framework 4.0,在4.5下貌似会报错,设置方法为项目上右键/属性页/找到左侧菜单栏里的生成/将框架版本改为4.0. 一.非空验证:Require ...
- linux commond
1 vi /etc/sysconfig/network-scripts/ifcfg-eth0 2 ifconfig 3 ping 172.22.14.59 4 ping 1 ...
- 盒模型Box Model(浮动)
一.标准盒模型的大小:border+padding+content(width) 怪异盒模型大小:padding+border 二.display inline 默认,且变为行由内 ...
- ServletContext当全局变量的使用
ServletContext对象 1,作用:JavaWeb应用的一个全局变量,一个应用只有一个ServletContext对象,在应用启动时,容器就会创建该对象 2,获得ServletContext对 ...
- 查看源代码查找获取sd卡剩余容量的代码
下载android源码,找到app下的Settings应用源码,导入Settings项目(android项目源码) 查找“可用空间”得到 <string name="memory_av ...
- Acitivity间数据的传递
使用startActivityForResult方法进行数据传递. MainActivity.java: public class MainActivity extends Activity { ...
- SQL Server 负载均衡集群方案之Moebius
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 架构原理(Architecture) 测试环境(Environment) 安装Moebius( ...