二维数组查找
二维数组查找
class Solution():
def chazhao2d(self, arr, m, n, p, q, tar) -> int:
if n >= m or q >= p:
mid_row = int(m + (n-m)/2)
mid_col = int(p + (q-p)/2)
if arr[mid_row][mid_col] == tar:
return (mid_row,mid_col)
elif arr[mid_row][mid_col] > x:
return Solution.chazhao2d(self,arr, m, mid_row-1, p, q, x) or Solution.chazhao2d(self,arr, mid_row, n, p, mid_col-1, x)
else:
return Solution.chazhao2d(self,arr, mid_row + 1,n, p, q, x) or Solution.chazhao2d(self,arr, m, mid_row, mid_col + 1, q, x)
return (-1,-1)
if __name__ == '__main__':
# print(bool([] or 2)) # 1
# print([] or 2) # 2
s = Solution()
tar = int(input())
M, N = tuple(map(int,input().strip().split()))
arr = [[0 for column in range(N)] for row in range(M)]
for i in range(M):
arr[i] = list(map(int,input().strip().split()))
kk = s.chazhao2d(arr, 0, len(arr)-1, 0, len(arr[0])-1, tar)
print(kk)
作者:楚千羽
出处:https://www.cnblogs.com/chuqianyu/
本文来自博客园,本文作者:楚千羽,转载请注明原文链接:https://www.cnblogs.com/chuqianyu/p/16247768.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利!