英文:
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 < 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;
}
The code is returning wrong answers.
答案1
得分: 0
根据您的测试案例,我认为您想要检查每一行是否已排序。
以下是更改内容:
- 将此处的
i < matrix.length - 1
改为i < matrix.length
以检查所有行。 - 将此处的
matrix[i][j] <= matrix[i + 1][j + 1]
改为matrix[i][j] <= matrix[i][j + 1]
以比较同一行的元素。 - 将此处的
j < matrix.length - 1
改为j < matrix[i].length - 1
。即使矩阵不是方阵,此更改将确保逻辑正常工作。
public boolean isSortedDescendant(int[][] matrix) {
if ((matrix.length == 0) || (matrix.length == 1)) {
return true;
} else {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length - 1; j++) {
if (matrix[i][j] <= matrix[i][j + 1]) {
return false;
}
}
}
}
return true;
}
还有一种更简单的方式来编写上述逻辑:
public boolean isSortedDescendant(int matrix[][]) {
for(int[] row: matrix)
for(int i = 1; i < row.length; i++)
if(row[i] >= 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
- This
i < matrix.length - 1
toi < matrix.length
to check all the rows. - This
matrix[i][j] <= matrix[i + 1][j + 1]
tomatrix[i][j] <= matrix[i][j + 1]
to compare the elements of the same row. - This
j < matrix.length - 1
toj < 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 < matrix.length; i++) {
for (int j = 0; j < matrix[i].length - 1; j++) {
if (matrix[i][j] <= 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 < row.length; i++)
if(row[i] >= row[i-1])
return false;
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论