1、从左下角开始查找,当目标元素大于当前元素,则向右查找;小于当前元素,则向上查找
public boolean Find(int target, int[][] array) { int m = array.length, n = array[0].length; for (int i = m - 1, j = 0; i >= 0 && j <= n - 1; ) { if (array[i][j] == target) return true; else if (array[i][j] < target) j++; else i--; } return false; }
2、从右上角开始查找
public boolean Find2(int target, int[][] array) { int i = array.length - 1, j = 0; while (i >= 0 && j <= array[0].length - 1) { if (array[i][j] < target) { j++; } else if (array[i][j] > target) { i--; } else { return true; } } return false; }
原文:https://www.cnblogs.com/Aug-20/p/11733971.html