题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
解决思路:递归
剑指offer代码:
- import java.util.ArrayList;
- import java.util.Collections;
- public class Solution {
- public ArrayList<String> Permutation(String str) {
- ArrayList<String> res=new ArrayList<String>();
- if(str.length()==0||str==null)return res;
- int n= str.length();
- helper(res,0,str.toCharArray());
- Collections.sort(res);
- return res;
- }
- public void helper( ArrayList<String> res,int index,char []s)
- {
- if(index==s.length-1)res.add(new String(s));
- for(int i=index;i<s.length;i++)
- {
- if(i