二维数组的查找

二维数组中的查找

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

代码实现

package 剑指offer;

/**
 * @author WangXiaoeZhe
 * @Date: Created in 2019/11/22 13:44
 * @description:
 */

public class Main1 {
    public static void main(String[] args) {

    }

    public boolean Find(int target, int[][] array) {
        int i = 0;
        int j = array[0].length - 1;
        while (i < array.length && j >= 0) {
            if (target < array[i][j]) {
                j--;
            } else if (target>array[i][j]){
                i++;
            }else if (target==array[i][j]){
                return true;
            }
        }
        return false;
    }
}
posted @ 2019-11-22 13:54  π。  阅读(136)  评论(0编辑  收藏  举报