运行时间:446ms
占用内存:5636k
#思路:一行一行顺序查找# -*- coding:utf-8 -*- class Solution: # array 二维列表 def Find(self, target, array): # write code here for i in range(len(array)): for j in range(len(array[0])): if target > array[i][j]: print("continue:",i,array[i][j]) continue elif target == array[i][j]: print("true:",array[i][j]) return True return False
2.根据数据规律快速查找
运行时间:364ms
占用内存:5752k
# -*- coding:utf-8 -*- ''' 思路 矩阵是有序的,从左下角来看,向上数字递减,向右数字递增, 因此从左下角开始查找,当要查找数字比左下角数字大时。右移 要查找数字比左下角数字小时,上移 ''' class Solution: # array 二维列表 def Find(self, target, array): # write code here rownum,colnum = len(array),len(array[0]) print(rownum,colnum) i,j = rownum-1,0 while i >= 0 and j < colnum: print(i,j,array[i],array[j]) if array[i][j] == target: return True elif array[i][j] > target: i -= 1 elif array[i][j] < target: j += 1 return False