[算法]排列組合算法

今天遇到需要編寫“排列組合算法”驾胆,雖然懂?dāng)?shù)學(xué)原理涣澡,但是真正寫的時(shí)候,還是吃不透丧诺。最后結(jié)合網(wǎng)上的代碼入桂,整理了出來(lái)。

Tip:文章最后關(guān)聯(lián)知乎一個(gè)帖子驳阎,介紹了什么是“排列組合”抗愁,如果不懂馁蒂,建議看看。


首先貼上js代碼:(原貼沒有任何注明蜘腌,所以也不知道出自哪位大佬)

package com.test;

public class Pailie {

public static void main(String[] args) {

? ? ? ? int[] ia = {1, 2, 3, 4,5,6,7,8,9,10};

? ? ? ? int n = 4;

? ? ? ? System.out.println("排列結(jié)果 : ");

? ? ? ? permutation("",ia, n);

// ? ? ? ? System.out.println("組合結(jié)果 : ");

// ? ? ? ? combination(ia, n);

? ? }

? ? public static void permutation(String s, int[] ia, int n) {

? ? ? ? if(n == 1) {

? ? ? ? ? ? for(int i = 0; i < ia.length; i++) {

? ? ? ? ? ? ? ? System.out.println(s+ia[i]);

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? for(int i = 0; i < ia.length; i++) {

? ? ? ? ? ? ? ? String ss = "";

? ? ? ? ? ? ? ? ss = s+ia[i]+"";

? ? ? ? ? ? ? ? //建立沒有第i個(gè)元素的子數(shù)組

? ? ? ? ? ? ? ? int[] ii = new int[ia.length-1];

? ? ? ? ? ? ? ? int index = 0;

? ? ? ? ? ? ? ? for(int j = 0; j < ia.length; j++) {

? ? ? ? ? ? ? ? ? ? if(j != i) {

? ? ? ? ? ? ? ? ? ? ? ? ii[index++] = ia[j];

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? permutation(ss, ii, n-1);

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? public static void combination(int[] ia, int n) {

? ? ? ? combination("", ia, n);

? ? }

? ? public static void combination(String s, int[] ia, int n) {

? ? ? ? if(n == 1) {

? ? ? ? ? ? for(int i = 0; i < ia.length; i++) {

? ? ? ? ? ? ? ? System.out.println(s+ia[i]);

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? for(int i = 0; i < ia.length-(n-1); i++) {

? ? ? ? ? ? ? ? String ss = "";

? ? ? ? ? ? ? ? ss = s+ia[i]+", ";

? ? ? ? ? ? ? ? //建立從i開始的子數(shù)組

? ? ? ? ? ? ? ? int[] ii = new int[ia.length-i-1];

? ? ? ? ? ? ? ? for(int j = 0; j < ia.length-i-1; j++) {

? ? ? ? ? ? ? ? ? ? ii[j] = ia[i+j+1];

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? combination(ss, ii, n-1);

? ? ? ? ? ? }

? ? ? ? }

? ? }

}


我項(xiàng)目用的TypeScript語(yǔ)言沫屡,所以我自己做了一遍翻譯,并且稍微改進(jìn)了一下撮珠,更加適配我的項(xiàng)目需要沮脖。

/** * 排列算法 * @param targetList 目標(biāo)列表(源數(shù)據(jù)列表) * @param step 步數(shù)(取幾個(gè)值,例如"Pm取n"中的"n") * @param totalList 總列表(總的數(shù)據(jù)列表) * @param pmtList 臨時(shí)排列列表(子列表) */ public static permutation<T>(targetList: T[], step: number, totalList: T[][], pmtList: T[] = []): void { if (step == 1) { for (let i = 0; i < targetList.length; i++) { let newPmtList: T[] = []; newPmtList = ObjectUtils.deepClone(pmtList, newPmtList); newPmtList.push(targetList[i]); totalList.push(newPmtList); //Tip:到這里代表一次結(jié)束。newPmtList就是排列出來(lái)的一種情況芯急。 } } else { for (let i = 0; i < targetList.length; i++) { let newPmtList: T[] = []; newPmtList = ObjectUtils.deepClone(pmtList, newPmtList); newPmtList.push(targetList[i]); //建立沒有第i個(gè)元素的子數(shù)組 let newTargetList: T[] = []; for (let j = 0; j < targetList.length; j++) { if (j != i) { newTargetList.push(targetList[j]); } } this.permutation(newTargetList, step - 1, totalList, newPmtList); } } }

??重新編輯帖子, 代碼就不能換行了, 自行修復(fù)一下吧.

排列算法使用方式:

let targetList = ["1", "2", "3", "a", "b", "c"];

let totalList = [];

permutation(targetList, 2, totalList);

console.log(">>>>? 測(cè)試總列表:", totalList);

/** * 組合算法 * @param targetList 目標(biāo)列表(源數(shù)據(jù)列表) * @param step 步數(shù)(取幾個(gè)值,例如"Cm取n"中的"n") * @param totalList 總列表(總的數(shù)據(jù)列表) * @param pmtList 臨時(shí)排列列表(子列表) */ public static combination<T>(targetList: T[], step: number, totalList: T[][], cbnList: T[] = []): void { if (step == 1) { for (let i = 0; i < targetList.length; i++) { let newCbnList: T[] = []; newCbnList = ObjectUtils.deepClone(cbnList, newCbnList); newCbnList.push(targetList[i]); totalList.push(newCbnList); //Tip:到這里代表一次結(jié)束勺届。newCbnList就是組合出來(lái)的一種情況。 } } else { for (let i = 0; i < targetList.length - (step - 1); i++) { let newCbnList: T[] = []; ObjectUtils.copyProperty(cbnList, newCbnList); newCbnList.push(targetList[i]); //建立從i開始的子數(shù)組 let newTargetList: T[] = []; for (let j = 0; j < targetList.length - i - 1; j++) { newTargetList.push(targetList[i + j + 1]); } this.combination(newTargetList, step - 1, totalList, newCbnList); } } }

??重新編輯帖子, 代碼就不能換行了, 自行修復(fù)一下吧.

組合算法使用方式:

let targetList = ["1", "2", "3", "a", "b", "c"];

let totalList = [];

combination(targetList, 2, totalList);

console.log(">>>>? 測(cè)試總列表:", totalList);



其實(shí)也沒修改什么特別的內(nèi)容志于,原作者是組合一串string輸出涮因,而我是需要取出各組結(jié)果废睦,所以改用泛型標(biāo)志伺绽,可以做成公用的工具類。

(Tip:大家可以參考嗜湃,結(jié)合自己需要另外改奈应,代碼要合適才好用。)


最后貼上一個(gè)介紹“排列組合”的帖子购披,個(gè)人覺得挺不錯(cuò)的杖挣。有興趣的小伙伴可以去看看,可以讓你很快理解排列組合刚陡。

https://www.zhihu.com/question/26094736

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末惩妇,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子筐乳,更是在濱河造成了極大的恐慌歌殃,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,406評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蝙云,死亡現(xiàn)場(chǎng)離奇詭異氓皱,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)勃刨,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門波材,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人身隐,你說(shuō)我怎么就攤上這事廷区。” “怎么了贾铝?”我有些...
    開封第一講書人閱讀 163,711評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵躲因,是天一觀的道長(zhǎng)早敬。 經(jīng)常有香客問(wèn)我,道長(zhǎng)大脉,這世上最難降的妖魔是什么搞监? 我笑而不...
    開封第一講書人閱讀 58,380評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮镰矿,結(jié)果婚禮上琐驴,老公的妹妹穿的比我還像新娘。我一直安慰自己秤标,他們只是感情好绝淡,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,432評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著苍姜,像睡著了一般牢酵。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上衙猪,一...
    開封第一講書人閱讀 51,301評(píng)論 1 301
  • 那天馍乙,我揣著相機(jī)與錄音,去河邊找鬼垫释。 笑死丝格,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的棵譬。 我是一名探鬼主播显蝌,決...
    沈念sama閱讀 40,145評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼订咸!你這毒婦竟也來(lái)了曼尊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,008評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤脏嚷,失蹤者是張志新(化名)和其女友劉穎骆撇,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體然眼,經(jīng)...
    沈念sama閱讀 45,443評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡艾船,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,649評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了高每。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片屿岂。...
    茶點(diǎn)故事閱讀 39,795評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖鲸匿,靈堂內(nèi)的尸體忽然破棺而出爷怀,到底是詐尸還是另有隱情,我是刑警寧澤带欢,帶...
    沈念sama閱讀 35,501評(píng)論 5 345
  • 正文 年R本政府宣布运授,位于F島的核電站烤惊,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏吁朦。R本人自食惡果不足惜柒室,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,119評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望逗宜。 院中可真熱鬧雄右,春花似錦、人聲如沸纺讲。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)熬甚。三九已至逢渔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間乡括,已是汗流浹背肃廓。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留粟判,地道東北人亿昏。 一個(gè)月前我還...
    沈念sama閱讀 47,899評(píng)論 2 370
  • 正文 我出身青樓峦剔,卻偏偏與公主長(zhǎng)得像档礁,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子吝沫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,724評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容