在一个m行n列二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维
数组和一个整数,判断数组中是否含有该整数。
使用Step-wise线性搜索。
```python
def get_value(l, r, c):
return l[r][c]
def find(l, x):
m = len(l) - 1
n = len(l[0]) - 1
r = 0
c = n
while c >= 0 and r <= m:
value = get_value(l, r, c)
if value == x:
return True
elif value > x:
c = c - 1
elif value < x:
r = r + 1
return False
```
例如:
pds=pd.DataFrame([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20],[21,22,23,24,25],[26,27,28,29,30],[31,32,33,34,35]])
print(pds)
# print(len(pds.index))
# print(len(pds.columns))
#找24
def get_value(pds,c,i):
return pds[c][i]
# def find(n,pds):
# c=len(pds.columns)-1
# i=len(pds.index)-1
# cc=0
# ii=i
# while ii>=0 and cc<=c:
# if n==get_value(pds,cc,ii):
# return True
# if n<get_value(pds,cc,ii):
# ii-=1
# if n>get_value(pds,cc,ii):
# cc+=1
def find(n,pds):
c=len(pds.columns)-1
i=len(pds.index)-1
cc=c
ii=0
while ii<=i and cc>=0:
if n==get_value(pds,cc,ii):
return True
if n<get_value(pds,cc,ii):
cc-=1
if n>get_value(pds,cc,ii):
ii+=1
print(find(24,pds))