剑指offer第28题:字符串的排列
2016-07-31 23:53
246 查看
输入:abc
输出:abc acb bac bca cab cba
思路:
字符串由两部分组成,第一个字符和后面的部分
1。先求所有可能出现在第一个位置的字符,
2。固定第一个字符,求后面字符的排列,递归
输出:abc acb bac bca cab cba
思路:
字符串由两部分组成,第一个字符和后面的部分
1。先求所有可能出现在第一个位置的字符,
2。固定第一个字符,求后面字符的排列,递归
public class Permutation { // 剑指offer第28题,字符串的排列 public void permutation(char[] ch){ if(ch == null || ch.length == 0){ return; } permutationString(ch,0); } private void permutationString(char[] ch,int index) { int end = ch.length - 1; if(index == end){ System.out.println(new String(ch)); }else { for(int i = index; i <= end; i ++){ char temp = ch[index]; ch[index] = ch[i]; ch[i] = temp; permutationString(ch,index+1); //恢复前面的变化 temp = ch[index]; ch[index] = ch[i]; ch[i] = temp; } } } public static void main(String[] args){ char[] example = {'a','b','c','d'}; new Permutation().permutation(example); } }
相关文章推荐
- 剑指Offer-28-字符串排列
- 剑指offer(24)-字符串的排列
- 【剑指Offer】字符串的排列
- 剑指offer-面试题28:字符串的排列
- 剑指offer 27 字符串的排列
- 剑指offer-题28:字符串的排列
- 剑指Offer28字符串的排列(递归和非递归实现)扩展有重复元素的排列,字符串的组合种类
- (C++)剑指offer-27:字符串的排列(分解让复杂问题简单)(没太懂)
- 剑指Offer之 - 字符串的排列
- 【剑指offer】面试题38:字符串的排列
- (二十)剑指offer之字符串的排列
- 【剑指offer】题28:字符串的排列
- 剑指offer-28字符串的排列
- 剑指offer25--字符串所以排列
- 【剑指offer-Java版】28字符串的排列
- 剑指offer之面试题28:字符串的排列
- 剑指offer——字符串的排列(好题,扩展题也很好,全排列的算法)
- 剑指offer-字符串的排列
- 剑指Offer:字符串的排列
- 剑指offer 面试题28—字符串的排列