字符串的排列
字符串的排列
http://www.nowcoder.com/practice/fe6b651b66ae47d7acce78ffdd9a96c7
Java的实现配合上牛客网无解的题目模板(返回值ArrayList<String>
)
import java.util.*; import java.util.stream.*; public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> ret = new ArrayList<>(); if (str == null || str.length() == 0) { return ret; } perm(0, str.toCharArray(), ret); List<String> res = ret.stream().distinct().sorted().collect(Collectors.toList()); return new ArrayList<>(res); } private void perm(int pos, char[] a, ArrayList<String> ret) { if (pos + 1 == a.length) { ret.add(new String(a)); return; } for (int i = pos; i < a.length; ++i) { char tmp = a[i]; a[i] = a[pos]; a[pos] = tmp; perm(pos + 1, a, ret); tmp = a[i]; a[i] = a[pos]; a[pos] = tmp; } } }