全排列--遞歸算法

          import java.util.ArrayList;
          import java.util.List;

          public class PaiLie {

          ?/**
          ? * @param args
          ? */
          ?public static void main(String[] args) {
          ??List<String> strList = new ArrayList<String>();
          ??strList.add("a");
          ??strList.add("b");
          ??strList.add("c");
          ??
          ??quanpailie(strList, 0);
          ?}
          ?
          ?/**
          ? * 按遞歸算法列出全排列
          ? * @param strList
          ? * @param cur
          ? */
          ?private static void quanpailie(List<String> strList, int cur)
          ?{
          ??if(cur >= strList.size()) {
          ???System.out.println(strList);
          ???return;
          ??}
          ???
          ??for(int i = cur; i < strList.size(); i++)
          ??{
          ???wape(strList, cur, i);
          ???quanpailie(strList, cur+1);
          ???wape(strList, cur, i);
          ??}
          ?}

          ?/**
          ? * 交換第i和j位置的值
          ? * @param strList
          ? * @param i
          ? * @param j
          ? */
          ?private static void wape(List<String> strList, int i,int j )
          ?{
          ??String temp = strList.get(i);
          ??strList.set(i, strList.get(j));
          ??strList.set(j, temp);
          ??
          ?}
          }

          posted on 2010-01-19 11:46 飛熊 閱讀(233) 評論(0)  編輯  收藏 所屬分類: 算法


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          <2010年1月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          收藏夾

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 晋州市| 屯留县| 泸溪县| 石家庄市| 齐齐哈尔市| 治县。| 清水县| 陕西省| 九龙城区| 罗江县| 普陀区| 来安县| 新乡县| 宣恩县| 射洪县| 竹山县| 咸宁市| 衡山县| 德兴市| 无锡市| 平陆县| 清徐县| 囊谦县| 云梦县| 赤城县| 板桥市| 永城市| 华池县| 德州市| 库伦旗| 鹤山市| 贡觉县| 东宁县| 台中县| 哈尔滨市| 什邡市| 庆阳市| 洛川县| 中阳县| 阿荣旗| 依兰县|