3--查找二维数组包含的数字

/**
     * 在一个二维数组中,每一行都按照从左往右递增的顺序排列,每一列都按照从上到下递增的顺序排列。
     * 请完成一个函数,输入这样的一个二维数组和整数,判断数组中是否存在该整数
     * 
     * 可从左下角(向上递减向右递增)或右上角(向下递增向左递减)开始查找 可将O(m*n)降为 O(max(m,n))
     */
    public static boolean findTarget(int[][] arrays,int target){
        int row=arrays.length-1;
        int column=arrays[0].length-1;
        int i=row,j=0;
        while(j<=column&&i>=0){
            if(target>arrays[i][j]){
                j++;
            }else if(target<arrays[i][j]){
                i--;
            }else{
                return true;
            }
        }
        return false;
    }

猜你喜欢

转载自blog.csdn.net/hxz6688/article/details/53193230