题目:
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
思路:
这个题目相对前面三题比较简单,假设该二维数组为下图(a),主要是思路上面过去,实现起来挺简单的,下面我介绍一下实现思路,有种思路是分成几部分去查找,但是会出现重叠的部分,因此可以考虑从右上角或者左上角进行比对,因为无论从左到右还是从上到下都是有序的,所以每一行的右边总比左边大,每一列的下面总比上边大,因此,找一个数的时候,假设是7,
从右上角开始比较,a[0][4] = 9 所以a[0] [4] 这一列所有的数都大于等于9,因此该列可以删除,同理,可以删除[3]列,得到(b)
依照同样的方法,从右上角开始删除行,可以删除【1】和【2】行。只剩下两行两列,比较右上角的数字,相等则返回
1 | 2 | 8 | 9 |
2 | 4 | 9 | 12 |
4 | 7 | 10 | 13 |
6 | 8 | 11 | 15 |
1 | 2 |
2 | 4 |
4 | 7 |
6 | 8 |
代码实现
/**
* @Description 二维数组中的查找
*
* @author hsk
*/
// 题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按
// 照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个
// 整数,判断数组中是否含有该整数。
public class FindInPartiallySortedMatrix {
/*
* 判断二维数组matrix中是否含有整数a
* 返回值为a的下标,{-1,-1}代表不存在
*/
public int[] find(int[][] matrix, int a) {
int[] index = { -1, -1 };
// 判断数组是否正确
if (matrix == null || matrix.length <= 0) {
System.out.println("数组无效!");
return index;
}
// 判断数组数字的大小是否符合大小规则
int columns = matrix[0].length;
for (int i = 0; i < matrix.length; i++) {
if (matrix[i].length != columns) {
System.out.println("数组列数不一致!");
return index;
}
for (int j = 0; j < matrix[i].length; j++) {
if (i == 0 && j == 0)
// matrix[0][0]不比较
break;
if (i == 0) { // 第一行的数,仅和前一列的数比较
if (matrix[i][j] < matrix[i][j - 1]) {
System.out.println("数组中数字大小不符合要求!");
return index;
}
} else if (j == 0) { // 第一列的数,仅和前一行的数比较
if (matrix[i][j] < matrix[i - 1][j]) {
System.out.println("数组中数字大小不符合要求!");
return index;
}
} else if (matrix[i][j] < matrix[i - 1][j] || matrix[i][j] < matrix[i][j - 1]) {
// 其余位置的数字,和前一行或前一列的比较
System.out.println("数组中数字大小不符合要求!");
return index;
}
}
}
// 正式查找
int row = 0; // 行数
int column = matrix[0].length - 1; // 列数
while (row <= matrix.length - 1 && column >= 0) {
if (a == matrix[row][column]) {
index[0] = row;
index[1] = column;
System.out.println("数字" + a + "在二维数组中的下标为:" + index[0] + "," + index[1]); // 注意下标是从0开始的
return index;
} else if (a < matrix[row][column]) {
column--;
} else {
row++;
}
}
System.out.println("数组中不含数字:" + a);
return index;
}
// ==================================测试代码==================================
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
public void test1() {
System.out.print("test1:");
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
int[] index = find(matrix, 7);
}
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数不在数组中
public void test2() {
System.out.print("test2:");
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
int[] index = find(matrix, 5);
}
// 数组为空
public void test3() {
System.out.print("test3:");
int[][] matrix = null;
int[] index = find(matrix, 7);
}
// 1 2 8 9
// 4 3 9 12
// 4 7 10 13
// 6 8 11 15
// 数组不满足大小规则
public void test4() {
System.out.print("test4:");
int[][] matrix = { { 1, 2, 8, 9 }, { 4, 3, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
int[] index = find(matrix, 7);
}
// 数组每行长度不一致
public void test5() {
System.out.print("test5:");
int[][] matrix = { { 1, 2, 8 }, { 4, 3, 9, 12 }, { 4, 7, 10 }, { 6, 8, 11, 15 } };
int[] index = find(matrix, 7);
}
public static void main(String[] args) {
FindInPartiallySortedMatrix f = new FindInPartiallySortedMatrix();
f.test1(); // 注意下标是从0开始的
f.test2();
f.test3();
f.test4();
f.test5();
}
}