public class Findin2D {
public static void main(String[] args){
int[][] array = new int[][]{{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
int target = 7;
System.out.print(new Findin2D().Find(target,array));
}
public boolean Find(int target, int[][] array) {
if (array.length == 0)
return false;
int col = 0;//列
int row = array[0].length - 1;//行
while (col <= array.length - 1 && row >= 0) {
if (target == array[col][row]) {
return true;
} else if (target > array[col][row]) {
col++;
} else {
row--;
}
}
return false;
}
}
剑指offer第一题:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
最新推荐文章于 2022-07-19 23:20:47 发布