今日写一个算法题目:二维数组中的查找(剑指Offer题目)
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
如:
找数据10,返回为true,且位于第三行第三列
考虑从右上角开始,用递归的方法找:
代码如下:
public class Test {
public static void main(String[] args) {
int[][] matrix = new int[][]{{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
System.out.println("从右上角开始,递归方法找数据: " + (10) + "是否找到: " + findDataRecursively(matrix, 10));
}
/* 从右上角开始,递归*/
public static boolean findDataRecursively(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return false;
}
return find(matrix, number, 0, matrix.length - 1);
}
public static boolean find(int[][] matrix, int number, int row, int column) {
if (row >= matrix.length || column < 0) {
return false;
}
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据,在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
return find(matrix, number, row--, column);//1
} else {
return find(matrix, number, row, column++);//2
}
}
}
执行程序,程序结果如下:
Exception in thread "main" java.lang.StackOverflowError
at 第二遍.Test.find(Test.java:30)
at 第二遍.Test.find(Test.java:30)
at 第二遍.Test.find(Test.java:30)
...
Process finished with exit code 1
看一遍程序的执行过程,没有异常,都是按照顺序执行的,后来debug一下,发现程序执行到 1 处,递归执行时,row并没有减一,导致每次执行递归,传入的参数一样,程序一直递归,导致虚拟机栈溢出。想到,++、--操作是在执行完毕之后才会++、--。即 前++是先自加再使用,而后++是先使用再自加。
比如:
执行结果 | |
int age=18; | |
int num=age++; | num=18,age=19 |
int num=++age; | num=19,age=19 |
int num=age--; | num=18, age=17 |
int num=--age; | num=17, age=17 |
所以后来改下代码:将1、2处的代码改为--row、++column即可
public class Test {
public static void main(String[] args) {
int[][] matrix = new int[][]{{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
System.out.println("从右上角开始,递归方法找数据: " + (10) + "是否找到: " + findDataRecursively(matrix, 10));
}
/* 从右上角开始,递归*/
public static boolean findDataRecursively(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return false;
}
return find(matrix, number, 0, matrix.length - 1);
}
public static boolean find(int[][] matrix, int number, int row, int column) {
if (row >= matrix.length || column < 0) {
return false;
}
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据,在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
return find(matrix, number, --row, column);//1
} else {
return find(matrix, number, row, ++column);//2
}
}
}
执行结果:
找到数据,在第 3 行,第 3 列
从右上角开始,递归方法找数据: 10是否找到: true
Process finished with exit code 0
后来继续写了,所有的实现方法如下:
/* 1.1 从右上角开始,递归*/
public static boolean findDataRightRecursively(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return false;
}
return findRight(matrix, number, 0, matrix.length - 1);
}
public static boolean findRight(int[][] matrix, int number, int row, int column) {
if (row >= matrix.length || column < 0) {
return false;
}
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据" + number + "在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
return findRight(matrix, number, ++row, column);
} else {
return findRight(matrix, number, row, --column);
}
}
/* 1.2 从右上角开始,循环*/
public static boolean findDataRightCircularly(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1) {
return false;
}
int row = 0;
int column = matrix[0].length - 1;
while (row < matrix.length && column >= 0) {
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据,在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
row++;
} else {
column--;
}
}
return false;
}
/* 2.1 从左下角开始,递归*/
public static boolean findDataLeftRecursively(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return false;
}
return findLeft(matrix, number, matrix.length - 1, 0);
}
public static boolean findLeft(int[][] matrix, int number, int row, int column) {
if (row >= matrix.length || column < 0) {
return false;
}
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据: " + number + " 在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
return findLeft(matrix, number, row, ++column);
} else {
return findLeft(matrix, number, --row, column);
}
}
/* 2.2 从左下角开始,循环*/
public static boolean findDataLeftCircularly(int[][] matrix, int number) {
if (matrix == null || matrix.length < 1) {
return false;
}
int row = matrix.length - 1;
int column = 0;
while (row >= 0 && column < matrix.length) {
int currentData = matrix[row][column];
if (currentData == number) {
System.out.println("找到数据: " + number + " 在第 " + (row + 1) + " 行,第 " + (column + 1) + " 列");
return true;
} else if (currentData < number) {
++column;
} else {
--row;
}
}
return false;
}
总结:以后写代码注意,只有必须后加时,把++写后面,其他写前面,养成这个习惯。细节毁所有。