排序 2 整数数组列表按降序交替排列



我试图以交替顺序将 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);
}

最新更新