簡體   English   中英

Java-以int數組的升序獲取索引

[英]Java - Obtain the indexes in ascending order of an int array

我有一個[2,4,1,0,0,3]的int數組,需要從中獲取升序的索引數組,表示[3,4,2,0,5,1]。

我嘗試通過使用排序數組來按順序獲取數字來解決此問題,然后在發生匹配時遍歷原始數組以查找索引。 如下:

public class IndexAscendingSorter {
    public static void main (String[] args) {
        int[] array = {2,4,1,0,0,3};
        IndexAscendingSorter finder = new IndexAscendingSorter();
        int[] indixes = finder.orderIndexAscending(array);

        System.out.println("Indexes of the array in ascending order: " +
                            Arrays.toString(indixes));
    }

    public int[] orderIndexAscending(int[] array) {
        int[] minimumIndexes = new int[array.length];
        int[] sortedArray = array.clone();
        Arrays.sort(sortedArray);

        for (int index = 0; index < array.length; index++){
            int minIndex = 0;
            for (int number : array) {
                if (number == sortedArray[index]) { 
                    minimumIndexes[index] = minIndex;
                    break;
                }
                minIndex++;
            }
        }
        return minimumIndexes;
    }
}

問題在於,對於相同的數字不會返回正確的索引,執行該代碼的輸出為:

數組的索引以升序排列:[3、3、2、0、5、1]第二個值array [1]應該是4而不是3。有人知道如何改善這一點嗎?

繼續您的方法,一種快速的解決方案是使用哈希集,在其中您將添加已經使用的索引,然后可以檢查它是否是重復索引。 只需將orderIndexAscending()函數更改為:

    public int[] orderIndexAscending(int[] array) {
        int[] minimumIndexes = new int[array.length];
        int[] sortedArray = array.clone();
        Arrays.sort(sortedArray);
        Set<Integer> savedIndexes = new HashSet<>();

        for (int index = 0; index < array.length; index++){
            int minIndex = 0;
            // Add the index in ascending order, we need to keep the indexes already
            // saved, so won't miss indexes from repeted values
            for (int number : array) {
                if (number == sortedArray[index] && !savedIndexes.contains(minIndex)) { 
                    savedIndexes.add(minIndex);
                    minimumIndexes[index] = minIndex;
                    break;
                }
                minIndex++;
            }
        }
        return minimumIndexes;
    }
}

將數字與原始索引配對:

[2,4,1,0,0,3] => [[2,0],[4,1],[1,2],[0,3],[0,4],[3,5]]]

然后按原始值排序:

=> [[0,3],[0,4],[1,2],[2,0],[3,5],[4,1]]]

最后提取索引:

=> [3,4,2,0,5,1]

將索引數組初始化為Integers 0、1、2、3,...,然后使用自定義編譯器對索引數組進行排序,該自定義編譯器查找相應的數組值並進行比較。

 for (int index = 0; index < array.length; index++){
        int minIndex = 0;
        for (int number : array) {
            if (number == sortedArray[index]) { 
                minimumIndexes[index] = minIndex;
                array[minIndex]=Integer.MAX_VALUE;
                break;
            }
            minIndex++;
        }
    }

您可以簡單地使用數組中從未存在的任何值來更新已訪問的值。

您可以簡單地使用數組中不存在的任何值來更新已經訪問過的值。

for (int index = 0; index < array.length; index++){
    int minIndex = 0;
    for (int number : array) {
        if (number == sortedArray[index]) { 
            minimumIndexes[index] = minIndex;
            array[minIndex]=Integer.MAX_VALUE;
            break;
        }
        minIndex++;
    }
}

暫無
暫無

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

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