package swordOffre.gaopengyu; public class FindIn2DimensionArray4 { public static void main(String[] args) { int[][] arr = new int[][]{{1, 2, 8, 9}, {2, 4, 9, 12}, {4, 7, 10, 13}, {6, 8, 11, 15}}; int target = 1; boolean result = FindIn2DimensionArray(arr, target, arr.length, arr[0].length); System.out.println(result); } public static boolean FindIn2DimensionArray(int[][] arr, int target, int rows, int cols) { boolean result = false; int row = 0; int col = cols - 1; while (row <= rows - 1 && col >= 0) { if (arr[row][col] == target) { result = true; break; } if (arr[row][col] > target) { col--; } if (arr[row][col] < target) { row++; } } return result; } }
二维数组中的查找4
最新推荐文章于 2022-05-06 22:24:02 发布