从对角考虑
1 package my_basic.class_3; 2 3 /** 4 * 从对角开始 5 */ 6 public class Code_09_FindNumInSortedMatrix { 7 public static boolean isContain(int[][] matrix,int k) { 8 int endR = matrix.length-1; 9 int endC = matrix[0].length - 1; 10 int row = endR; 11 int column = 0; 12 while (row > -1 && column <= endC) { 13 if (matrix[row][column] == k) { 14 return true; 15 }else if (matrix[row][column] > k) { 16 row--; 17 }else { 18 column++; 19 } 20 } 21 return false; 22 } 23 24 public static void main(String[] args) { 25 int matrix[][]= new int[][] { 26 { 0, 1, 2, 3, 4, 5, 6 },// 0 27 { 10, 12, 13, 15, 16, 17, 18 },// 1 28 { 23, 24, 25, 26, 27, 28, 29 },// 2 29 { 44, 45, 46, 47, 48, 49, 50 },// 3 30 { 65, 66, 67, 68, 69, 70, 71 },// 4 31 { 96, 97, 98, 99, 100, 111, 122 },// 5 32 { 166, 176, 186, 187, 190, 195, 200 },// 6 33 { 233, 243, 321, 341, 356, 370, 380 } 34 }; 35 int k = 233; 36 System.out.println(isContain(matrix,k)); 37 } 38 }