[英]Given an array of integers as input, return true if the array is sorted. Note that the array can be sorted in either ascending or descending order
Sample Input #1 样本输入#1
isSorted([1,3,5,7})
Sample Output #1 样本输出#1
true
Sample Input #2 样本输入2
isSorted({11,9,2,-5})
Sample Output #2 样本输出2
true
Sample Input #3 样本输入#3
isSorted({1,2,3,4,-1,-2})
public boolean isSorted(int[] arr){
boolean isSorted = false;
if(arr.length==1)
return true;
for(int i=0;i<arr.length-1;i++)
{
if(arr[i]<arr[i+1])
{
isSorted = true;
}
else if(arr[i]>arr[i+1])
{
isSorted = true;
}
else
isSorted = false;
if(isSorted != true)
return isSorted;
}
return isSorted;
}
What happens to my code some of my testcase didn't pass parameter '{24,27,30,31,34,37,40,42}' pass parameter '{1,3,5,7,4}' fail 我的代码发生什么情况,一些测试用例未通过参数'{24,27,30,31,34,37,40,42}'通过参数'{1,3,5,7,4}'失败
You are trying to use a single boolean
variable to keep two bits of information, which does not work. 您正在尝试使用单个
boolean
变量来保留两位信息,这是行不通的。 You need two boolean
s here - one to indicate that the array is sorted in ascending order, and another one to indicate that the array is sorted in descending order. 您在此处需要两个
boolean
-一个用于指示该数组以升序排序,另一个用于指示该数组以降序排序。
Prepare two variables, isAscending = true
and isDescending = true
. 准备两个变量,
isAscending = true
和isDescending = true
。 Go through the array the way you do now, and set the corresponding variable to false
if you detect an inversion. 按照现在的方式遍历数组,如果检测到反转,则将相应的变量设置为
false
。 Never set these variables to true
again, because a single inversion breaks the sort order. 永远不要将这些变量再次设置为
true
,因为单个反转会破坏排序顺序。
if (arr[i]<arr[i+1]) isDescending = false;
if (arr[i]>arr[i+1]) isAscending = false;
That's all you need to do. 这就是您需要做的。 Once the loop is over,
isAscending || isDescending
循环结束后,
isAscending || isDescending
isAscending || isDescending
expression gives you the answer. isAscending || isDescending
表达式为您提供了答案。
To speed up an exit from your for
loop, use this termination condition: 要加快退出
for
循环的速度,请使用以下终止条件:
i<arr.length-1 && (isAscending || isDescending)
Logic: 逻辑:
I have a different proposition than the one from Andreas : 我的提议与Andreas的提议不同:
ascending
and descending
to true
ascending
和descending
为true
ascending = ascending && previousElement <= currentElement
ascending = ascending && previousElement <= currentElement
ascending || descending
ascending || descending
ascending || descending
You need another var to indicate the sorting:ascending or descending 您需要另一个var来指示排序:升序还是降序
short srt = 0;//-1 descending, +1ascending
then compare the two first val to decide the sorting sense, so I propose this solution: 然后比较两个第一个val以确定排序意义,因此我提出了以下解决方案:
public boolean isSorted(int[] arr){
short srt = 0;//-1 descending, +1ascending
if(arr.length <= 2)
return true;
else {
if(arr[0] < arr[1])
srt = 1;
else
srt = -1
for(int i=1;i<arr.length-1;i++)
if((arr[i] < arr[i+1] && srt = -1) || (arr[i] > arr[i+1] && srt = 1)
return false;
}
return true;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.