【題目描述】數組
【源代碼】bash
思路1:時間複雜度0(nlogn)
class Solution:
# array 二維列表
def Find(self, target, array):
# write code here
if(len(array)==0 or len(array[0])==0):return False
for i in range(0,len(array)):
if(array[i][0]>target):
break
num=array[i]
left=0
right=len(num)-1
while left<=right:
mid=int((left+right)/2)
if(num[mid]>target):
right=mid-1
elif(num[mid]<target):
left=mid+1
else:
return True
return False
複製代碼
思路2:時間複雜度:O(n)
class Solution:
# array 二維列表
def Find(self, target, array):
# write code here
rows = len(array) - 1
cols= len(array[0]) - 1
i = rows
j = 0
while j<=cols and i>=0:
if target<array[i][j]:
i -= 1
elif target>array[i][j]:
j += 1
else:
return True
return False
複製代碼