在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
found = False
row = 0
rows = len(array) #得到数组的行数
columns = len(array[0])
if ((isinstance(array, list) and len(array) != 0) and rows > 0 and columns > 0): #判断数组是否为空
column = columns - 1
while(row < rows and column >= 0):
if (array[row][column] == target):
found = True
break
else:
if (array[row][column] > target):
column = column - 1
else:
row = row + 1
return found

本文介绍了一种在特定排序规则下的二维数组中查找特定整数的方法。通过定义一个解决方案类及Find方法,采用行增列减的策略高效定位目标值。
758

被折叠的 条评论
为什么被折叠?



