[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(),也可以自己写一个.对于这种看起来简单的题目,应该在能优化的地方, ...
随机推荐
- 如何在Xilinx ISE中使用TCL提高工作效率
http://wenku.baidu.com/link?url=jxtsPLGUlWwYuD8TtfWYYU_NhY5Qty3rx8ZDLCkINLe39JRGb90V5HoJhnkn9r_PQ6vZ ...
- oracle中decode()函数
简单写写,后续继续补充
- android webview乱码问题
使用 loadData方法是中文部分会出现乱码,即使指定“utf-8”.“gbk”.“gb2312”也一样. webView.getSettings().setDefaultTextEncodingN ...
- python学习第二天:数字与字符串转换及逻辑值
1.数字与字符串的转化 #1.数字转字符,使用格式化字符串: *1.demo = ‘%d’ % source *2.%d整型:%f 浮点型 :%e科学计数 ...
- vim 高级使用技巧第二篇
上篇我贴上了我使用的vim配置及插件配置,有这些东西只能是一个脚本堆积,无从谈高效的代码阅读开发. 下面我们就来写经常使用的命令,就从配置F系列快捷键开始吧. F+ n 快捷键配置 F1基本上时帮助, ...
- 从V$SQL_PLAN中FORMAT执行计划
10G版本 select sql_id from v$sqlarea where sql_text like'%xxx%'; select * from v$sql_plan where sql_i ...
- StrHelper
public class StrHelper { private static string passWord; //加密字符串 /// <summary> /// 判断输入是否数字 // ...
- java多线程之停止线程
/*1.让各个对象或类相互灵活交流2.两个线程都冻结了,就不能唤醒了,因为根据代码要一个线程活着才能执行唤醒操作,就像玩木游戏3.中断状态就是冻结状态4.当主线程退出的时候,里面的两个线程都处于冻结状 ...
- makefile--Unfound symbol
Unfound symbol ,库函数的包含问题或者头文件包含问题 makefile对#的识别度太低了,如果使用了,#它之后的可能就不能识别了,然后会报错的Unfound symbol /////// ...
- python isinstance 判断各种类型的小细节
1. 基本语法 isinstance(object, classinfo) Return true if the object argument is an instance of the class ...