利用本身有的特性:
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
self.target=target
self.array=array
n=len(self.array)
m=len(self.array[0])
flag=False
if (len(self.array[0]) == 0):
return False
if(self.target<self.array[0][0]) or (self.target>self.array[n-1][m-1]):
return False
for i in range(n-1,-1,-1):
if self.target<=self.array[i][n-1] and self.target>=self.array[i][0]:
for j in range(len(self.array[0])-1,-1,-1):
if self.target==self.array[i][j]:
return True
return Flase
~~~
利用2分查找
~~~python
class Solution:
# array 二维列表
def Find(self, target, array):
self.target=target
self.array=array
n=len(self.array)
m=len(self.array[0])
flag=False
if (len(self.array[0]) == 0):
return False
if(self.target<self.array[0][0]) or (self.target>self.array[n-1][m-1]):
return False
for i in range(0,n):
l=0
r=n-1
while(l<=r):
mid=(l+r)//2
if(self.target<array[i][mid]):
r=mid-1
elif(self.target>array[i][mid]):
l=mid+1
else:
return True
return False
网友评论