簡體   English   中英

降序交替排列2個整數數組列表

[英]Sorting 2 Integer array list in alternating while descending order

我試圖將2個數組以交替的順序組合在一起,並進行排序,以想象具有交替顏色的金字塔(您不能交換相同的顏色),並且內部的值是寬度。

WhiteA{18,16,11,4,3,2}
BlackB{13,8,6}

輸出應為

{18,13,11,8,4}

16被跳過是因為16> 13,所以下一個是11,而6被省略了,因為最后一個將是雙色

for (int i = 0; i < positive.size(); i++) {
    for (int n = 0; n < negative.size(); n++) {

        if (positive.get(i) > Math.abs(negative.get(n))) {
            count.add(positive.get(i));
            if (positive.get(i) < Math.abs(negative.get(n))) {
                count.add(negative.get(n));

            }
        }

    }

}

正只表示白色負只表示黑色

我期望輸出應該是{18,13,11,8,4}但是當我嘗試我的代碼時,我會得到{18,18,18,16,16,16,11,11}

我會嘗試更改循環類型,並通過開關(例如i%2)交替檢查哪個數組,將正/負數組的第一個元素與計數數組中的最后一個元素進行比較。

比較之后:如果該值較小,則將其添加到count數組中,然后從正/負數組中刪除該元素。

抱歉:我不在電腦上進行測試。

int ai = 0;
int bi = 0;
List<Integer> a = Arrays.asList(18,16,11,4,3,2); // this is your first list
List<Integer> b = Arrays.asList(13,8,6); // this is your second list
List<Integer> count = new ArrayList<>();
while (ai < a.size() && bi < b.size()) {
    int current = a.get(ai);
    count.add(current);
    while (bi < b.size() && b.get(bi) > current) {
        ++bi;
    }
    if (bi == b.size()) {
        break;
    }
    current = b.get(bi);
    count.add(current);
    while (ai < a.size() && a.get(ai) > current) {
        ++ai;
    }
    if (ai == a.size()) {
        break;
    }
}

看起來問題在於您如何使用嵌套循環,在負循環內,您總是將其與正循環的相同值進行比較,直到負循環遍歷所有元素之后,它才會遞增,因此重復的數字。

另外,在正值大於負值的檢查中會發生正值是否小於負值的檢查,因此它將永遠不會觸發。

自從我使用Java以來​​已經有一段時間了,但是我沒有在那種環境中進行測試的工具,但是下面的代碼似乎可以在python中解決問題:

count = []

positive = [18, 16, 11, 4, 3, 2]
negative = [13, 10, 6]

lastGrabPos = False

positiveIndex = 0
negativeIndex = 0

while positiveIndex < len(positive) and negativeIndex < len(negative):
    if positive[positiveIndex] > negative[negativeIndex]:
        if not lastGrabPos:
            count.append(positive[positiveIndex])
            lastGrabPos = True

        positiveIndex += 1


    if positive[positiveIndex] < negative[negativeIndex]:
        if lastGrabPos:
            count.append(negative[negativeIndex])
            lastGrabPos = False
        negativeIndex += 1

if not lastGrabPos:
    count.append(positive[positiveIndex])

else:
    count.append(negative[negativeIndex])

我相信是翻譯成Java ...

int positiveIndex = 0;
int negativeIndex = 0;

Boolean lastGrabPos = false;

while(positiveIndex < positive.size() && negativeIndex < negative.size()) {
    if (positive.get(positiveIndex) > Math.abs(negative.get(negativeIndex))){
        if(!lastGrabPos){
            count.add(positive.get(positiveIndex));
            lastGrabPos = true;
        }
        positiveIndex += 1;
    }

    if (positive.get(positiveIndex) < Math.abs(negative.get(negativeIndex))){
        if(lastGrabPos){
            count.add(negative.get(negativeIndex));
            lastGrabPos = false;
        }
        negativeIndex += 1;
    }

}

if(!lastGrabPos){
    count.add(positive.get(positiveIndex));
}
else{
    count.add(negative.get(negativeIndex));
}

for(Integer element : count) {
    System.out.println(element);
}

暫無
暫無

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

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