二维数组的查找

题目描述

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

题目详解

从左下角或者右上角开始搜索,如果大于或小于该数就可以去除掉某一列。

 

完整程序

public class Solution {
    public boolean Find(int target, int [][] array) {
        Integer len_scale = array.length;
        Integer len = array[0].length;
        Integer i = 0;
        Integer j = len_scale-1;
        while(i<len && j >=0){
            if(array[i][j] == target){
                return true;
            }else if (array[i][j] > target){
                j--;
            }else{
                i++;
            }
        }
        return false;
    }
}

  

posted @ 2020-03-20 11:55  xxcnotes  阅读(112)  评论(0)    收藏  举报