I need a code that return true if all elemnts of a matrix is sorted from highest to lowest

huangapple go评论63阅读模式
英文:

I need a code that return true if all elemnts of a matrix is sorted from highest to lowest

问题

我需要一个代码,如果矩阵的所有元素按从高到低的顺序排列,则返回 true。我指的是如果矩阵的所有元素都是降序的,比如 3-2-1,它应该返回 true;如果是 3-1-2,则应返回 false。如果矩阵是空的,则应返回 true。在任何其他情况下,应返回 false。矩阵是由此测试形成的。

@Test
public void testSortedDescendantMatrixRows() {
    FirstSteps firstSteps = new FirstSteps();
    int[][] matrix1 = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}};
    assertTrue(firstSteps.isSortedDescendant(matrix1));
    int[][] matrix2 = {{3, 2, 2}, {5, 4, 3}, {8, 7, 6}};
    assertFalse(firstSteps.isSortedDescendant(matrix2));
    int[][] matrix3 = {{3, 2, 1}};
    assertTrue(firstSteps.isSortedDescendant(matrix3));
    int[][] matrix4 = {{}};
    assertTrue(firstSteps.isSortedDescendant(matrix4));
    int[][] matrix5 = {{5, 4, 3, 2, 1}, {12, 5, 4, 3}, {34, 12, 10, 9, 8, 7, 6}};
    assertTrue(firstSteps.isSortedDescendant(matrix5));
}

我的代码是:

public boolean isSortedDescendant(int[][] matrix) {
    if ((matrix.length == 0) || (matrix.length == 1)) {
        return true;
    } else {
        for (int i = 0; i < matrix.length - 1; i++) {
            for (int j = 0; j < matrix.length - 1; j++) {
                if (matrix[i][j] <= matrix[i + 1][j + 1]) {
                    return false;
                }
            }
        }
    }
    return true;
}

该代码返回错误的答案。

英文:

I need a code that returns true if all elements of a matrix is sorted from highest to lowest By that i mean that if all elements of a matrix are in an decending order, like 3-2-1 it should return true if 3-1-2 it should return false. If the matrix is empty then it should return true. In any other scenario it should return false. Matrices are formed by this test.

@Test
    public void testSortedDescendantMatrixRows() {
        FirstSteps firstSteps = new FirstSteps();
        int[][] matrix1 = {{3, 2, 1}, {5, 4, 3}, {8, 7, 6}};
        assertTrue(firstSteps.isSortedDescendant(matrix1));
        int[][] matrix2 = {{3, 2, 2}, {5, 4, 3}, {8, 7, 6}};
        assertFalse(firstSteps.isSortedDescendant(matrix2));
        int[][] matrix3 = {{3, 2, 1}};
        assertTrue(firstSteps.isSortedDescendant(matrix3));
        int[][] matrix4 = {{}};
        assertTrue(firstSteps.isSortedDescendant(matrix4));
        int[][] matrix5 = {{5, 4, 3, 2, 1}, {12, 5, 4, 3}, {34, 12, 10, 9, 8, 7, 6}};
        assertTrue(firstSteps.isSortedDescendant(matrix5));
    }

My code is

public boolean isSortedDescendant(int[][] matrix) {
        if ((matrix.length == 0) || (matrix.length == 1)) {
            return true;
        } else {
            for (int i = 0; i &lt; matrix.length - 1; i++) {
                for (int j = 0; j &lt; matrix.length - 1; j++) {
                    if (matrix[i][j] &lt;= matrix[i + 1][j + 1]) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

The code is returning wrong answers.

答案1

得分: 0

根据您的测试案例,我认为您想要检查每一行是否已排序。

以下是更改内容:

  1. 将此处的 i &lt; matrix.length - 1 改为 i &lt; matrix.length 以检查所有行。
  2. 将此处的 matrix[i][j] &lt;= matrix[i + 1][j + 1] 改为 matrix[i][j] &lt;= matrix[i][j + 1] 以比较同一行的元素。
  3. 将此处的 j &lt; matrix.length - 1 改为 j &lt; matrix[i].length - 1。即使矩阵不是方阵,此更改将确保逻辑正常工作。
public boolean isSortedDescendant(int[][] matrix) {
    if ((matrix.length == 0) || (matrix.length == 1)) {
        return true;
    } else {
        for (int i = 0; i &lt; matrix.length; i++) {
            for (int j = 0; j &lt; matrix[i].length - 1; j++) {
                if (matrix[i][j] &lt;= matrix[i][j + 1]) {
                    return false;
                }
            }
        }
    }
    return true;
}

还有一种更简单的方式来编写上述逻辑:

public boolean isSortedDescendant(int matrix[][]) {
    for(int[] row: matrix)
        for(int i = 1; i &lt; row.length; i++)
            if(row[i] &gt;= row[i-1])
                return false;
                
    return true;
}
英文:

Based on your test cases, I think you want to check if each row is sorted or not.

Here are the changes

  1. This i &lt; matrix.length - 1 to i &lt; matrix.length to check all the rows.
  2. This matrix[i][j] &lt;= matrix[i + 1][j + 1] to matrix[i][j] &lt;= matrix[i][j + 1] to compare the elements of the same row.
  3. This j &lt; matrix.length - 1 to j &lt; matrix[i].length - 1. It will ensure the logic works even if the matrix is not a square matrix
public boolean isSortedDescendant(int[][] matrix) {
    if ((matrix.length == 0) || (matrix.length == 1)) {
        return true;
    } else {
        for (int i = 0; i &lt; matrix.length; i++) {
            for (int j = 0; j &lt; matrix[i].length - 1; j++) {
                if (matrix[i][j] &lt;= matrix[i][j + 1]) {
                    return false;
                }
            }
        }
    }
    return true;
}

There is a simpler way to write the above logic

public boolean isSortedDescendant(int matrix[][]) {
    for(int[] row: matrix)
        for(int i = 1; i &lt; row.length; i++)
            if(row[i] &gt;= row[i-1])
                return false;
                
    return true;
}

huangapple
  • 本文由 发表于 2020年8月21日 19:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63521777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定