打印string所有排列

Below are the permutations of string ABC.
ABC ACB BAC BCA CBA CAB

idea: 交换位置,当index == last index of string时说明已经到了最后一位,可以打印

code:
private void helper(String s, int start) {
if (start == s.length() - 1) {
System.out.println(s);
} else {

        for (int i = start; i < s.length(); i++) {
            //核心三句话
            s = swap(s, start, i);
            helper(s, start + 1);
            s = swap(s, start, i);
        }
    }
}

你可能感兴趣的:(打印string所有排列)