剑指offer1 二维数组查找python求解及变形题求解
刷题记录:切勿眼高手低
暴力法:
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
for arr in array:
if target in arr:
return True
return False
二分法:
class Solution:
# array 二维列表
def Find(self,target, array):
# write code here
col = len(array[0])-1
rows =len(array)
row = 0
if not array:
return False
while row<rows and col >= 0:
if array[row][col]<target:
row+=1
if row < rows://此判断目的在于其可以处理非对称的二维数组
col=len(array[row])-1
else:
return False
elif array[row][col]>target:
col-=1
else:
return True
return False