我需要编写一个方法,它接受二维数组'int[][] m'和一个值 'val'并检查val是否在数组中,其时间复杂度为 O(n),其中n定义为行数,并且m必须为平方。
可以用作我的方法参数的数组必须对这个方法返回true:
(如果它返回true,那么数组符合要求)
我的方法应该在数组中包含
可以用作我的方法参数的数组必须对这个方法返回true:
(如果它返回true,那么数组符合要求)
public static boolean test(int[][] m) {
int n = m.length;
for (int r = 0; r < (n - 1); r++)
for (int c = 0; c < n; c++)
for (int i = 0; i < n; i++)
if (m[r][c] > m[r + 1][i]) return false;
return true;
}
这个数组返回TRUE:
int [][] arr3 = new int [][]{
{ 0, 2, 1, 2, 0, 5, 5, 5, },
{ 21, 21, 7, 7, 7, 21, 21, 21 ,},
{ 21, 21, 21, 21, 21, 21, 21 , 21, },
{ 21, 21, 23 , 42, 41, 23, 21, 21, },
{ 60 ,56, 57, 58, 53, 52, 47, 51 ,},
{ 61, 65, 70 , 72, 73, 78, 82, 98 ,},
{ 112, 121, 112, 134, 123, 100, 98, 111,},
{ 136, 136, 136, 134, 147, 150, 154, 134,},
};
我的方法应该在数组中包含
val
并且像这样返回true:public boolean findValTest(int [][] m, int val){...}
val
在哪里? - oleg.cherednik