/**
* @author danny
* @create 2019-05-25 21:59
* <p>
* 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
* 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
*/
public class demo1 {
public static void main(String[] args) {
int[][] array = {{1, 2, 8, 9},
{2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}};
System.out.println(Find(7, array));
}
public static boolean Find(int target, int[][] array) {
/**
* 从左下角的那个数字考虑:
* 往上递减
* 往右递增
*
*/
int row = array.length - 1;
int column = 0;
while (row >= 0 && column < array[0].length) {
if (array[row][column] > target) {
row--;
} else if (array[row][column] < target) {
column++;
} else if (array[row][column] == target) {
return true;
}
}
return false;
}
}