美文网首页
打印string所有排列

打印string所有排列

作者: weiyongzhiaaa | 来源:发表于2020-04-14 15:32 被阅读0次

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所有排列

      本文链接:https://www.haomeiwen.com/subject/ffltvhtx.html