题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
例如下面的二维数组就是每行、每列都递增排序。如果在这个数组中查找数字7,则返回true;如果查找数字5,由于数组不含有该数字,则返回false。
首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除这个数字所在的列;如果该数字小于要查找的数字,剔除这个数字所在的行。也就是说如果要查找的数字不在数组的右上角,则每一次都在数组的查找范围中剔除一行或者一列,这样每一步都可以缩小查找的范围,直到找到要查找的数字,或者查找范围为空。
例如,我们要在上述的二维数组中查找数字7的步骤如下图所示:
public static boolean Find(int target, int [][] array) {
if (null == array || array.length == 0 || array[0].length == 0){
return false;
}
//二维数组的行数
int rows = array.length - 1;//为了定位找到最右上角元素
int columns = array[0].length;
int column = 0;
while ((rows >= 0) && (column < columns)){
//如果选中的右上角元素元素 > 要查找的数字
//剔除该元素所在的列- rows--
if (array[rows][column] > target){
rows--;
//如果选中的右上角元素元素 < 要查找的数字
//剔除该元素所在的行- column++
}else if (array[rows][column] < target){
column++;
}else {
//如果选中的右上角元素元素 = 要查找的数字
//即找到了
return true;
}
}
//否则,就是没找到
return false;
}
在前面的分析中,我们每一次都是选取数组查找范围内的右上角数字。同样,我们也可以选取左下角的数字。但我们不能选择左上角或者右下角。以左上角为例,最初数字1位于初始数组的左上角,由于1小于7,那么7应该位于1的右边或者下边。此时我们既不能从查找范围内剔除1所在的行,也不能剔除1所在的列,这样我们就无法缩小查找的范围。
选择右上角比较方便
package SwordOffer;
import org.junit.Test;
/**
* @author kankan
* @creater 2019-10-17 8:51
*/
public class Solution1 {
public static boolean Find(int target, int [][] array) {
if (null == array || array.length == 0 || array[0].length == 0){
return false;
}
//二维数组的行数
int rows = array.length - 1;//为了定位找到最右上角元素
int columns = array[0].length;
int column = 0;
while ((rows >= 0) && (column < columns)){
//如果选中的右上角元素元素 > 要查找的数字
//剔除该元素所在的列- rows--
if (array[rows][column] > target){
rows--;
//如果选中的右上角元素元素 < 要查找的数字
//剔除该元素所在的行- column++
}else if (array[rows][column] < target){
column++;
}else {
//如果选中的右上角元素元素 = 要查找的数字
//即找到了
return true;
}
}
//否则,就是没找到
return false;
}
//(1)要查找的数字在数组中
@Test
public void FindTest1(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(7, matrix));
}
//(2)要查找的数不在数组中
@Test
public void FindTest2(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(5, matrix));
}
//(3)要查找的数是数组中最小的数字
@Test
public void FindTest3(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(1, matrix));
}
//(4)要查找的数是数组中最大的数字
@Test
public void FindTest4(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(15, matrix));
}
//(5)要查找的数比数组中最小的数字还小
@Test
public void FindTest5(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(0, matrix));
}
//(6)要查找的数比数组中最大的数字还大
@Test
public void FindTest6(){
// 1 2 8 9
// 2 4 9 12
// 4 7 10 13
// 6 8 11 15
// 要查找的数在数组中
int[][] matrix = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };
System.out.println(Solution1.Find(16, matrix));
}
//(7)鲁棒性测试,输入空指针
@Test
public void FindTest7(){
System.out.println(Solution1.Find(16, null));
}
}