public class FindNum { public static boolean findANum(int[][] array, int target) { int row = array.length;//行数 int cloumn = array[0].length;//列数 int i = 0; int j = cloumn - 1; boolean found = false; while(i <= row-1 && j >= 0) { if(target < array[i][j]) { j--; } if(target > array[i][j]) { i++; } if(target == array[i][j]) { found = true; break; } } return found; } public static void main(String[] args) { int a[][] = {{1,2,8,9},{2,4,9,12},{4,7,10,13}}; System.out.println(a.length); System.out.println(a[0].length); System.out.println(findANum(a, 7)); } }
本文介绍了一个高效的二维数组查找算法,该算法能够在有序的二维数组中快速定位目标数值。通过对比数组右上角元素与目标值,逐步缩小搜索范围,直至找到目标或确定目标不存在。文章提供了详细的Java实现代码,并通过实例验证了算法的有效性。
4213

被折叠的 条评论
为什么被折叠?



