题目描述
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
解题思路:从二维数组的左下角开始查找改整数,若array[i][][j] > target,向上移动,若array[i][][j] < target,向右移动。
Python代码如下:
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
if not array or not array[0]:
return False
m,n = len(array),len(array[0])
i,j = m-1,0
while i >= 0 and j < n:
if array[i][j] == target:
return True
elif array[i][j] > target:
i -= 1
else:
j += 1
return False