全排列Permutations
Posted 李雷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全排列Permutations相关的知识,希望对你有一定的参考价值。
描述
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]
代码
package com.lilei.myes.es.pack1107; public class quanpailie { public static void main(String[] args) { char[] cs = new char[] { ‘a‘, ‘b‘, ‘c‘,‘d‘ }; pailie(cs, 0); } public static void pailie(char[] cs, int e) { if (e == cs.length) { System.out.println(new String(cs)); } else { for (int i = e; i < cs.length; i++) { swap(cs, i, e); pailie(cs, e + 1); swap(cs, i, e); } } } static void swap(char[] cs, int a, int b) { char tmp = cs[a]; cs[a] = cs[b]; cs[b] = tmp; } }
以上是关于全排列Permutations的主要内容,如果未能解决你的问题,请参考以下文章
[Leetcode 46]全排列 Permutations 递归
LeetCode 46. 全排列(Permutations)
LeetCode 47. 全排列 II(Permutations II)