第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

生成多個(gè)列表中的所有組合

生成多個(gè)列表中的所有組合

生成多個(gè)列表中的所有組合給定未知數(shù)量的列表,每個(gè)列表具有未知長度,我需要生成具有所有可能的唯一組合的單個(gè)列表。例如,給出以下列表:X: [A, B, C] Y: [W, X, Y, Z]然后我應(yīng)該能夠生成12種組合:[AW, AX, AY, AZ, BW, BX, BY, BZ, CW, CX, CY, CZ]如果添加了3個(gè)元素的第三個(gè)列表,我將有36個(gè)組合,依此類推。關(guān)于如何用Java做到這一點(diǎn)的任何想法?(偽代碼也可以)
查看完整描述

3 回答

?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊

你需要遞歸:

假設(shè)您的所有列表都在lists,這是一個(gè)列表列表。讓我們result列出您所需的排列。你可以像這樣實(shí)現(xiàn)它:

void generatePermutations(List<List<Character>> lists, List<String> result, int depth, String current) {
    if (depth == lists.size()) {
        result.add(current);
        return;
    }

    for (int i = 0; i < lists.get(depth).size(); i++) {
        generatePermutations(lists, result, depth + 1, current + lists.get(depth).get(i));
    }}

最終的電話會(huì)是這樣的:

generatePermutations(lists, result, 0, "");


查看完整回答
反對(duì) 回復(fù) 2019-08-27
?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

這個(gè)話題派上了用場(chǎng)。我用Java完全重寫了以前的解決方案,更加用戶友好。此外,我使用集合和泛型來獲得更大的靈活性:

/**
 * Combines several collections of elements and create permutations of all of them, taking one element from each
 * collection, and keeping the same order in resultant lists as the one in original list of collections.
 * 
 * <ul>Example
 * <li>Input  = { {a,b,c} , {1,2,3,4} }</li>
 * <li>Output = { {a,1} , {a,2} , {a,3} , {a,4} , {b,1} , {b,2} , {b,3} , {b,4} , {c,1} , {c,2} , {c,3} , {c,4} }</li>
 * </ul>
 * 
 * @param collections Original list of collections which elements have to be combined.
 * @return Resultant collection of lists with all permutations of original list.
 */public static <T> Collection<List<T>> permutations(List<Collection<T>> collections) {
  if (collections == null || collections.isEmpty()) {
    return Collections.emptyList();
  } else {
    Collection<List<T>> res = Lists.newLinkedList();
    permutationsImpl(collections, res, 0, new LinkedList<T>());
    return res;
  }}/** Recursive implementation for {@link #permutations(List, Collection)} */private static <T> void permutationsImpl(List<Collection<T>> ori, Collection<List<T>> res, int d, List<T> current) {
  // if depth equals number of original collections, final reached, add and return
  if (d == ori.size()) {
    res.add(current);
    return;
  }

  // iterate from current collection and copy 'current' element N times, one for each element
  Collection<T> currentCollection = ori.get(d);
  for (T element : currentCollection) {
    List<T> copy = Lists.newLinkedList(current);
    copy.add(element);
    permutationsImpl(ori, res, d + 1, copy);
  }}

我正在使用guava庫來創(chuàng)建集合。


查看完整回答
反對(duì) 回復(fù) 2019-08-27
?
蠱毒傳說

TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超3個(gè)贊

此操作稱為笛卡爾積。Guava提供了一個(gè)實(shí)用功能:Lists.cartesianProduct


查看完整回答
反對(duì) 回復(fù) 2019-08-27
  • 3 回答
  • 0 關(guān)注
  • 648 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)