赞
踩
List<String> res = new ArrayList<>(); char []c; public String[] permutation(String s) { c = s.toCharArray(); change(0); return res.toArray(new String[res.size()]); } public void change(int x){ // x 表示现在本次固定的位数 if(x==c.length-1){ res.add(String.valueOf(c)); return; } Set<Character> set = new HashSet<>(); for(int i = x;i<c.length;i++){ if(set.contains(c[i])) continue; set.add(c[i]); //交换位置 swap(i,x); //递归固定下一位 change(x+1); //再交换回来 swap(i,x); } } public void swap(int a,int b){ char temp = c[a]; c[a] = c[b]; c[b] = temp; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。