有序指的是每行从左到右依次变大,每列从上到下依次变大
思路: 从右上顶点开始依次判断当前值与给定值的大小,往左下顶点移动,结束条件是下标超过范围
public class FindNumInOrderMatr { public static void main(String[] args) { int[][] matrix = new int[][] { { 10, 12, 13, 15, 16, 17, 18 }, { 23, 24, 25, 26, 27, 28, 29 }, { 44, 45, 46, 47, 48, 49, 50 }, { 65, 66, 67, 68, 69, 70, 71 }, { 96, 97, 98, 99, 100, 111, 122 }, { 166, 176, 186, 187, 190, 195, 200 }, { 233, 243, 321, 341, 356, 370, 380 } }; int k = 96; System.out.println(isContains(matrix, k)); } private static boolean isContains(int[][] matrix, int k) { int aR = 0; int aC = matrix[0].length -1 ; while(aR <= matrix.length-1 && aC >= 0) { if(matrix[aR][aC] > k) { aC --; }else if(matrix[aR][aC] < k) { aR ++; }else { return true; } } return false; } }