package Offer;import com.sun.org.apache.xalan.internal.xsltc.trax.SmartTransformerFactoryImpl;//二维数组中的查找//巧妙地选取位置,使得每一次比较后,都能缩小查询的范围//可以每次选取右上角或者左下角/*1 2 8  9
2 4 9  12
4 7 10 13
6 8 11 15 */public class Test4 {//枚举右上角的值public boolean findNumberIn2DArray(int[][] matrix, int target) {if (matrix == null || matrix.length <= 0 || matrix[0].length <= 0) {return false;
        }int row = matrix.length, col = matrix[0].length;int sx = 0, sy = col - 1;//每次枚举右上角的值while (sx < row && sy >=0) {if (matrix[sx][sy] == target) {return true;
            } else if (matrix[sx][sy] > target) {   //当前位置比target大,删除当前位置所在的列sy--;
            } else {    //当前位置比target小,删除当前位置所在的行sx++;
            }
        }return false;
    }//枚举左下角的值public boolean findNumberIn2DArray2(int[][] matrix, int target) {if (matrix == null || matrix.length <= 0 || matrix[0].length <= 0) {return false;
        }int row = matrix.length, col = matrix[0].length;int sx = row - 1, sy = 0;//每次枚举左下角的值while (sx >=0 && sy < col) {if (matrix[sx][sy] == target) {return true;
            } else if (matrix[sx][sy] > target) {   //"删除当前行"sx--;
            } else {
                sy++;
            }
        }return false;
    }
}