/*** *在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。 * 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。 */ public class TArraySortFind { public static void main(String[] args) { int[][] t = new int[][]{{1, 2, 3, 4, 5}, {6, 10, 11, 12, 13}, {7, 14, 15, 16, 17}, {8, 18, 19, 20, 21}, {9, 22, 23, 24, 25}}; System.out.println("reslut: " + Solution.Find(11, t)); } } /** * 复杂度 log(n) */ class Solution { public static boolean Find(int target, int [][] array) { int cloumn = array[0].length - 1; //一行最大长度 int rowMax = array.length - 1;//最大行数 int row = 0;//行 if (array[0].length==0){ return false; } //小于左上角或者大于右下角,都不存在 if ((target > array[rowMax][cloumn] || target < array[0][0]) || array[0].length == 0) { return false; } while (row <= rowMax && cloumn >= 0) { int v = array[row][cloumn]; if (v == target) {//0行最大列开始 return true; }else if(target<array[row][cloumn]){//小于向左走 cloumn--; }else {//大于向下走 row++; } } return false; } }
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组
最新推荐文章于 2021-01-14 11:02:26 发布