标签:
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; public class Solution { HashSet<String> arrangeSet = new HashSet<>(); public void DfsStr(char str[],char sequence[],int step,int strLen,boolean moveFlag[]) { if(step == strLen) { String arrange = new String(sequence); arrangeSet.add(arrange); return; } for(int i = 0; i < strLen; i++) { if(moveFlag[i] == false) { sequence[step] = str[i]; moveFlag[i] = true; DfsStr(str, sequence, step + 1, strLen, moveFlag); moveFlag[i] = false; //回退一步 } } } public ArrayList<String> Permutation(String str) { char sequence[] = new char[str.length()]; boolean moveFlag[] = new boolean[str.length()]; char strArrary[] = str.toCharArray(); ArrayList<String> stringList = new ArrayList<>(); if(str == null || str.isEmpty()) { return stringList; } for(int i = 0; i < str.length(); i++) { moveFlag[i] = false; } DfsStr(strArrary, sequence, 0, str.length(), moveFlag); stringList.addAll(arrangeSet); Collections.sort(stringList); return stringList; } }
标签:
原文地址:http://www.cnblogs.com/daimadebanyungong/p/4964488.html