[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(),也可以自己写一个.对于这种看起来简单的题目,应该在能优化的地方, ...
随机推荐
- innodb对update的处理
当更新非聚集索引上记录 和 聚集索引上的主键时,是标记删除,然后插入新的记录 当更新聚集索引上的非主键列时,是updated-in-place,也就是说原地修改,不会插入新记录. 之前一直以为都是以标 ...
- js原生bind()用法[注意不是jquery里面的bind()]
<div id="a"> <div></div> <div></div> <div></div> ...
- PullToRefresh的个性化扩展
一:实现区别下拉刷新和上拉加载 参考资料:http://blog.csdn.net/losetowin/article/details/18261389 在PullToRefresh的类库的com.h ...
- Leetcode 102. Binary Tree Level Order Traversal(二叉树的层序遍历)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 第五章 jQuery中的动画
通过jQuery中的动画方法,能轻松地为网页添加精彩的视觉效果,给用户一种全新体验. 1.show()方法和hide()方法 该方法的功能与css()方法设置display属性效果相同. 给show( ...
- ASP.Net Core 运行在Linux(CentOS)
Linux Disibutaion:CentOS 7.1 Web Server:Apache.Kestrel 1.安装.net core sudo yum install libunwind libi ...
- arguments 函数内部属性
1.arguments 是在function方法里面的,是实参数组,用法是挺多的,下面来记录一下 2.利用arguments实现方法的重载 //01.使用argument模拟方法重载 function ...
- C#学习笔记3:提示“截断字符串或二进制数据”错误解决方法
1.调试程序如出现“截断字符串或二进制数据”的关于数据库的错误,可以先试一试修改数据库中字符定义的长度. 2.使用ManualResetEvent前需导入 命名空间System.Threading; ...
- (九)Hibernate 检索策略
所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 这里的hibernate.cfg.xml配置信息我就不再写了 第一节:检 ...
- ###《Machine Learning》by Andrew NG
点击查看Evernote原文. #@author: gr #@date: 2014-10-17 #@email: forgerui@gmail.com Fundamental 一. 矩阵的迹.秩 矩阵 ...