[剑指Offer] 4.二维数组的查找
1. 直接遍历
# 借助于in # -*- coding:utf-8 -*- class Solution: # array 二维列表 def Find(self, array, target): # write code here flag = False for index in range(len(array)): if target in array[index]: flag = True return flag
2. 从最右上角开始
小了,去列;
大了,加行
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*- class Solution: # array 二维列表 def Find(self, array, target): # write code here #标识变量 flag = False #检查输入 if array ==None: return Flag nrow = len(array) ncol = len(array[0]) #右上角位置 row = 0 col = col-1 # 从右上角遍历 while (row<nrow) and (col<ncol) : if array[row][col]== target: flag = True break elif array[row][col]>target: col =col - 1 else: row = row + 1 return flag