剑指offer面试题目【4】--二维数组中的查找

题目描述

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

Python代码

# -*- coding:utf-8 -*-
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        xend=len(array)-1
        yend=len(array[0])-1
        x=0
        while x<=xend and yend>=0:
            if array[x][yend]==target:
                return True
            elif array[x][yend]>target:
                yend -= 1
            else:
                x += 1
        return False
            
            

猜你喜欢

转载自blog.csdn.net/weixin_42702666/article/details/88573098