二维数组查找

二维数组查找

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)
posted @ 2022-05-09 02:27  楚千羽  阅读(21)  评论(0编辑  收藏  举报