打印字符的所有排列組合

給定一個字符串,輸出字符串中字符的所有排列組合

 public static void permutation(char[] str, int i) {
        if (i >= str.length)
            return;
        if (i == str.length - 1) {
            System.out.println(String.valueOf(str));
        } else {
            for (int j = i; j < str.length; j++) {
                swap(str, i, j);
                permutation(str, i + 1);
                swap(str, i, j);
            }
        }
    }

 public void swap(char[] c, int i, int j){
    temp = str[j];
    str[j] = str[i];
    str[i] = temp; 
 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章