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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用不同的“r”數(shù)字制作所有字符串組合?

如何使用不同的“r”數(shù)字制作所有字符串組合?

梵蒂岡之花 2021-12-30 16:40:29
我想編寫一個函數(shù),它接受一個“整數(shù)”和一個“字符串?dāng)?shù)組”,然后返回所有可能的組合。考慮到所有字符串都是不同的這是一個例子:ArrayList<Strings> strings={a,b,c}int n= strings.size();k= 1;String sol="";while(k!=n+1){sol+=function(strings,k);//<== this is the problemk++;}String[] solution= sol.split(" ");并在刪除重復(fù)元素之后://Solution expected:={a,b,c,ab,bc,ac,abc} //the elements should not be repeated
查看完整描述

1 回答

?
慕雪6442864

TA貢獻(xiàn)1812條經(jīng)驗(yàn) 獲得超5個贊

現(xiàn)在這是獲取字符串排列的一個有趣的轉(zhuǎn)折,說起來容易做起來難。無論如何,如果您確實(shí)有一種方法可以根據(jù)為每個排列提供的最小/最大長度值為您提供所需的排列,那么實(shí)現(xiàn)起來相對簡單,而且我確信僅StackOverflow就有很多這樣的代碼示例。


通常排列是在簡單的字符串上完成的,"abc"而不是包含在某種類型的單個字符/字符串集合中的元素,如{'a', 'b', 'c'}或{"a", "b", "c"}。為了有效地獲得所需的排列,您需要將 ArrayList 元素轉(zhuǎn)換為字符串,以便{"a", "b", "c"}將"abc". 除非每個元素實(shí)際上包含多個字符串字符,否則在集合中繼續(xù)操作是沒有意義的,因此將集合轉(zhuǎn)換為字符串是有意義的。


首先,您需要一種執(zhí)行字符串排列的方法,我確信 StackOverflow 中有很多方法可以解決。在任何情況下,我都會為您提供我對排列方法的再現(xiàn),該方法允許您提供一個字符串,提供每個排列所需的字符長度,并刪除重復(fù)項(xiàng)(或不刪除)。該方法只接受一個字符串,以保持方法的靈活性。然而,它確實(shí)返回一個 String 的 ArrayList,您當(dāng)然可以通過對代碼進(jìn)行一些修改來更改為返回您喜歡的任何內(nèi)容。如果您確實(shí)更改了代碼,請記住該方法是遞歸的:


/**

 * This method will generate all possible permutations of the supplied input

 * string and return all those permutations within an String

 * ArrayList.<br><br>

 *

 * @param inputString     (String) The string to acquire permutations

 *                        from.<br>

 *

 * @param length          (Integer) The desired min/max string length of

 *                        returned permutations. In other words, if you only

 *                        want all permutations consisting of 3 characters

 *                        then supply the number 3. If you want all possible

 *                        permutations to be returned then supply the number

 *                        0.<br>

 * 

 * @param removeDuplicates (Boolean) Supply boolean true to ensure there will 

 * be no duplicate permutations within the returned ArrayList. Supply false if 

 * duplicates are desired. You can get duplicate permutations if there are 

 * duplicate characters within the supplied input string, for example:<pre>

 * 

 *      "aabbcc"

 * 

 * can return these permutations: 

 * 

 *      a, a, b, b, c, c            1 Character Permutations. See the Duplicates?

 * 

 *      aa, ab, ac, aa, ab, ac,     2 Character Permutations

 *      ba, bb, bc, ba, bb, bc,         See the Duplicates?

 *      ca, cb, cc, ca, cb, cc

 *

 *      aab, aac, aba, abb, abc,    3 Character Permutations

 *      aca, acb, acc, aab, aac,        See the Duplicates?

 *      aba, abb, abc, aca, acb, 

 *      acc, baa, bab, bac, bba, 

 *      bbc, bca, bcb, bcc, baa, 

 *      bab, bac, bba, bbc, bca, 

 *      bcb, bcc, caa, cab, cac, 

 *      cba, cbb, cbc, cca, ccb, 

 *      caa, cab, cac, cba, cbb, 

 *      cbc, cca, ccb

 * 

 * However if boolean true is supplied to remove duplicates the results would 

 * be:

 *      

 *      a, b, c                     1 Character Max Permutations. No Duplicates.

 * 

 *      aa, ab, ac, ba, bb, bc,     2 Character Max Permutations

 *      ca, cb, cc                          No Duplicates

 *      

 *      aab, aac, aba, abb, abc,    3 Character Max Permutations

 *      aca, acb, acc, baa, bab,            No Duplicates

 *      bac, bba, bbc, bca, bcb, 

 *      bcc, caa, cab, cac, cba, 

 *      cbb, cbc, cca, ccb</pre>

 * 

 * 

 * @param recursiveResult (String) FOR INTERNAL RECURSIVE USE ONLY! DO NOT

 *                        SUPPLY A ARGUMENT HERE!<br>

 *

 * @return (String ArrayList)

 */

public ArrayList<String> getPermutations(String inputString, final int length, 

        boolean removeDuplicates, String... recursiveResult) {

    String currentResult = "";

    if (recursiveResult.length > 0) {

        currentResult = recursiveResult[0];

    }

    //Convert the inputString to a ArrayList of characters...

    ArrayList<Character> possibleChars = new ArrayList<>(inputString.length());

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

        possibleChars.add(inputString.charAt(i));

    }


    ArrayList<String> result = new ArrayList<>(possibleChars.size());

    for (char append : possibleChars) {

        String permutation = currentResult + append; //create a new string with an additional character

        if (permutation.length() == length || length == 0) {

            result.add(permutation); //add the permutation to the result

        }

        if (possibleChars.size() > 0) {

            //make a new list with the appendable characters

            ArrayList<Character> possibleCharsUpdated = (ArrayList) possibleChars.clone();

            //from that list, exclude the character we just appended

            possibleCharsUpdated.remove(new Character(append));

            //Convert the new character ArrayList to a String

            //of characters for the recursion...

            String passToRecursion = "";

            for (int i = 0; i < possibleCharsUpdated.size(); i++) {

                passToRecursion += possibleCharsUpdated.get(i);

            }


            //merge the result of a recursive call of this method and the result we already had

            result.addAll(getPermutations(passToRecursion, length, true, permutation));


        }

    }

    // Remove duplicates if desired...

    // LinkedHashSet doesn't allow for Duplicates 

    // and automatically removes them.

    if (removeDuplicates) {

        ArrayList<String> tmpArray = new ArrayList<>(new LinkedHashSet<>(result));

        result.clear();

        result.addAll(tmpArray);

        tmpArray.clear();

    }

    return result;

}

現(xiàn)在要從您提供的內(nèi)容中獲得您想要的結(jié)果,它會是這樣的:


// Create String ArrayList

ArrayList<String> strings = new ArrayList<>();

strings.add("a"); strings.add("b"); strings.add("c");


// Convert ArrayList to a simple string

String stringToPermutate = String.join("", strings);


// Hold the number of elements within the strings ArrayList

int n = strings.size();


// Counter for while loop condition which will

// ultimately determine the Permutation Character

// Length for each iteration within the WHILE loop.

int k = 1;


// Prepare to build a result string that will hold

// the result from each call to the getPermutations

// method (which by the way is a recursive method).

StringBuilder sol = new StringBuilder();


while (k <= n) {

    // Call method to permutate our simple string based

    // on the Permutation Character Length stipulated by k

    ArrayList<String> permutations = getPermutations(stringToPermutate, k, true);


    // Convert ArrayList to a comma (, ) delimited string

    String listToString = String.join(", ", permutations); 


    // Ternary used here instead of IF/ELSE

    sol.append(sol.toString().equals("") ? listToString : ", " + listToString);


    // Increment counter

    k++;

}


// Split the contents of sol into a string Array

String[] solution = sol.toString().split(", ");


// Print solution String Array to Console window.

System.out.println(Arrays.toString(solution));

這是您最終應(yīng)該在控制臺窗口中顯示的內(nèi)容:


[a, b, c, ab, ac, ba, bc, ca, cb, abc, acb, bac, bca, cab, cba]


查看完整回答
反對 回復(fù) 2021-12-30
  • 1 回答
  • 0 關(guān)注
  • 157 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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