简体   繁体   English

给定一个整数数组作为输入,如果该数组已排序,则返回true。 请注意,数组可以按升序或降序排序

[英]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 = trueisDescending = 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: 逻辑:

  • Loop through list with an index counter 使用索引计数器遍历列表
  • First, forward index while next two values are equal 首先,前进索引,而后两个值相等
  • If next value is less than current value, do descending check 如果下一个值小于当前值,则进行降序检查
  • If next value is greater than current value, do ascending check 如果下一个值大于当前值,则进行升序检查
  • Verify that all remaining values are ascending/descending, or equal 验证所有剩余值都在升/降或相等

I have a different proposition than the one from Andreas : 我的提议与Andreas的提议不同:

  • Initialize booleans ascending and descending to true 初始化布尔值ascendingdescendingtrue
  • Store item 0, iterate from item 1. While either or the booleans is true, store the current value and increment the index. 存储项目0,从项目1开始进行迭代。当布尔值为true或true时,存储当前值并递增索引。 During each loop, update the two booleans (for example for the first one : ascending = ascending && previousElement <= currentElement 在每个循环中,更新两个布尔值(例如第一个布尔值: ascending = ascending && previousElement <= currentElement
  • return 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM