备战秋招,因为我数据结构刚看数组和链表,我就先分类刷数组和链表的题目,我贼菜,所以什么复杂度我就不大会,方法也就是那种比较容易理解的方法,嘿嘿. /** * 在一个二维数组中(每个一维数组的长度相同), * 每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。 * 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 */ public class Practice1 { public static void main(String[] args) { Practice1 p = new Practice1(); int[][] array = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}}; int target = 5; boolean b = p.Find(target, array); System.out.println(b); } public boolean Find(int target, int[][] array) { //行 int row = array.length; //列 int col = array[0].length; //从最后一列第一行开始找 int i = row - 1; int j = 0; // 不能从第一行第一列开始找,因为当小于target的时候,往上走往右走都可以 //判断条件要写好 while (i >= 0 && j < col) { if (array[i][j] == target) { return true; } else if (array[i][j] < target) { //小于就移动列 j++; } else if (array[i][j] > target) { //大于就移动行 i--; } } //没有就返回false return false; } }
剑指offer-第1题-二维数组中的查找
最新推荐文章于 2025-08-17 21:40:23 发布