上一次,已经发过一贴..
我想说的是:我不是上来要代码的.因为百度到处都是.
我需要的是大家能放上来一个自己理解的代码..然后能尽可能的给我讲清楚...这个知识点困扰了
很久..比方说:全排列的思路就是递归.那样说了跟没说对我来说都一样的..
希望大家能帮个忙.

解决方案 »

  1.   


    大家可以放出一个 自己写的代码. 然后就以 abc 3个字符为例子 把程序流程和思路给我说下.
      

  2.   


    这个是上个帖子别人给的:
    public class AllSort
    {
    public static void main(String[] args) {
    char buf[] = {'a','b','c'};
    perm(buf, 0, buf.length -1);

    }

    public static void perm(char[] buf, int start, int end){
    if(start == end){
    for(int i = 0;i <= end; i++){
    System.out.print(buf[i]);
    }
    System.out.println();
    }else{
    for(int i=start; i<=end; i++){
    System.out.println("start: " + start + "-->"+buf[start]);
    char temp = buf[start];
    buf[start] = buf[i];
    buf[i] = temp;
    System.out.println("-"+buf[start]+"--start----");
    perm(buf, start + 1, end);
    System.out.println("---end----");
    temp = buf[start];
    buf[start] = buf[i];
    buf[i] = temp;
    }
    }
    }}我给加入了标志.结果如下:
    /*
    start: 0-->a
    -a--start----
    start: 1-->b
    -b--start----
    abc
    ---end----
    start: 1-->b
    -c--start----
    acb
    ---end----
    ---end----
    start: 0-->a
    -b--start----
    start: 1-->a
    -a--start----
    bac
    ---end----
    start: 1-->a
    -c--start----
    bca
    ---end----
    ---end----
    start: 0-->a
    -c--start----
    start: 1-->b
    -b--start----
    cba
    ---end----
    start: 1-->b
    -a--start----
    cab
    ---end----
    ---end----
    */但我还是没能理解 为什么会产生这个结果 以及全排列成功
      

  3.   


    start: 0-->a
    -a--start----
    start: 1-->b
    -b--start----
    abc
    ---end----
    start: 1-->b
    -c--start----
    acb
    ---end----
    ---end----
    /*
     其实我写了 只是我不知道这个思路是怎么想的...
     比如单纯从您代码的运行结果来说吧: 
     第一次 start = 0; i = start = 0;
     第二次 start = 1; i = start = 1;
    所以start: 0-->a  -a--start---- 
        start: 1-->b  -b--start----
        这会start = start+1 = 2 == end然后就打印出abc.到此为止我理解了...*/
    但再往下---->
    start: 1-->b
    -c--start----
    acb
    ---end----
    ---end----
    /*
    这会start 也就是第一次递归完毕(就应该循环第二下 i = start + 1) start应该是2了啊 怎么还是1呢?*/
      

  4.   

    我给你一个 我自己写的 很简短public class Arrangement { public static void go(int[] data) {
    go(data, new boolean[data.length], new int[data.length], 0);
    } public static void go(int[] data, boolean[] state, int[] flag, int p) {
    print(p, data, flag); for (int i = 0; i < data.length; i++) {
    if (!state[i]) {
    state[i] = true;
    flag[p] = i;
    go(data, state, flag, p + 1);
    state[i] = false;
    }
    }
    } public static void print(int p, int[] data, int[] flag) {
    if (p >= data.length) {
    for (int i = 0; i < data.length; i++) {
    System.out.print(data[flag[i]] + " ");
    }
    System.out.println();
    }
    } public static void main(String[] args) {
    int[] data = { 1, 2, 3 };
    go(data);
    }
    }