全排列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]

代码

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
    }
 
}

 

  

 

posted on   李雷  阅读(201)  评论(0编辑  收藏  举报

导航

< 2025年1月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示