簡體   English   中英

如何置換int []並將每個int []保存到int [] []中?

[英]How can I permute an int[] and save each int[] into an int[][]?

我需要在int[][]排列第一行,並將每個結果排列保存在后續行中。 我的教授給了我這個實現的字符串版本作為指導:

private static void permute( char [ ] str, int low, int high )
{
        if( low == high )
        System.out.println( str );
    for( int i = low; i <= high; i++ )
    {
        char [ ] tmp = str.clone( ); // tmp will be str
        tmp[ i ] = str[ low ]; // with i and low
        tmp[ low ] = str[ i ]; // swapped
        permute( tmp, low + 1, high );
    }
}

到目前為止,這是我所擁有的(第一位是我的稱呼方式):

for (int row = 0; row < arr.length * LIST_SIZE; row++){
        if (arr.length % LIST_SIZE == LIST_SIZE-1)
            arr[row] = permute(arr[row%LIST_SIZE], 0, LIST_SIZE-1).clone();
    }

public int[] permute(int[] intArr, int low, int high){
    int[] returnArr = null;

    if (low == high){
        returnArr = intArr.clone();
    }

    for (int i = low; i <= high; i++){
        int[] temp = intArr.clone();
        temp[i] = intArr[low];
        temp[low] = intArr[i];
        permute(temp, low+1, high);
    }

    return returnArr;
}

現在,第一個數組之后的2d數組的每一行僅填充零。 我有種感覺,我正在使這個工作變得比現在困難得多。

您忘了實際使用從permute()返回的置換值,請替換

permute(temp, low+1, high);

returnArr = permute(temp, low+1, high);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM