gpt4 book ai didi

java - 数组IntList(java)

转载 作者:行者123 更新时间:2023-12-02 07:14:57 25 4
gpt4 key购买 nike

Write a method isPairwiseSorted that returns whether or not a list of integers is pairwise sorted (true if it is, false otherwise). A list is considered pairwise sorted if each successive pair of numbers is in sorted (non-decreasing) order.

我编写了以下方法,但它总是返回 true。请在这里需要一些帮助。

public boolean isPairwiseSorted() {
boolean isFalse=false;
for(int i = 0; i < size; i+=2){
if( elementData[i] < elementData[i+1] || size == 0 || size == 1){
return true;
}
}
return isFalse;
}

最佳答案

if (size == 0 || size == 1)
return true;

for (int i = 0; i < size - 1; i += 2)
{
if (elementData[i] > elementData[i+1])
return false;
}

return true;

关于java - 数组IntList(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com