【Java】 剑指offer(3) 二维数组中的查找

题目:

       在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

实现:

//思路:从右上角,第一行最后一列开始查找,一次向左,或向下继续查找
public class Solution {
    public boolean Find(int target, int [][] array) {
        int a=array.length;//行
        int b= array[0].length;//列
        int m=0; //行索引
        int n=b-1;//列索引
        while( n>=0 && m<a){
            int temp=array[m][n];
            if (temp==target)
                return true;
            else if (temp<target)
                m++;
            else
                n--;
        }
        return false;
    }

}

 

posted @ 2019-08-13 22:57  Temprol  阅读(105)  评论(0编辑  收藏  举报