12次元配列で検索
ネット
パイソンからの牛
2次元配列(各1次元配列の長さは同じ)では、各行は左から右に昇順でソートされ、各列は上から下に昇順でソートされます。関数を完成させ、そのような2次元配列と整数を入力して、配列に整数が含まれているかどうかを判別してください。
# -*- coding:utf-8 -*-
class Solution:
# array 二维列表
def Find(self, target, array):
# write code here
self.w=len(array[0])-1
self.h=len(array)-1
''' 右上'''
w = self.w
h = 0
while h<=self.h and w>=0:
print(h,w,array[h][w],target)
if array[h][w]<target:
h=h+1
continue
elif array[h][w]>target:
w=w-1
continue
else:
return True
return False
''' 左下 '''
h = self.h
w = 0
while w<=self.w and h>=0:
print(h,w,array[h][w],target)
if array[h][w]<target:
w=w+1
continue
elif array[h][w]>target:
h=h-1
continue
else:
return True
return False
# array=[[1,5,6,10,18,24],[2,6,9,11,19,25],[3,7,10,12,20,26],[4,8,22,25,27,28]]
# array=[[1,2,3,4],[2,3,4,8],[4,6,9,15]]
array=[
[0, 1, 3, 4, 5, 7, 8, 11, 13, 15, 18, 21, 24, 27, 30, 32, 35, 36, 39, 41, 42, 43, 46, 49, 52, 55, 58, 60, 63, 66, 67, 69, 72, 75, 78, 80, 81, 82, 85, 86],
[1, 4, 6, 8, 11, 12, 15, 17, 18, 20, 23, 24, 27, 30, 33, 34, 38, 39, 42, 44, 47, 48, 51, 52, 55, 57, 59, 62, 64, 67, 70, 72, 75, 77, 81, 83, 84, 87, 90, 91],
[4, 7, 8, 11, 14, 16, 18, 20, 21, 24, 27, 29, 32, 35, 36, 39, 40, 42, 44, 46, 49, 52, 54, 56, 58, 60, 61, 64, 67, 70, 73, 76, 78, 81, 84, 87, 89, 91, 93, 96],
[5, 8, 10, 13, 15, 19, 21, 23, 24, 27, 29, 31, 34, 37, 38, 41, 43, 45, 46, 49, 52, 55, 58, 59, 61, 64, 67, 69, 71, 72, 76, 78, 79, 83, 87, 90, 91, 94, 96, 97],
[6, 11, 14, 16, 18, 22, 24, 27, 29, 32, 33, 35, 36, 40, 42, 44, 47, 50, 51, 52, 54, 58, 60, 62, 64, 67, 70, 73, 76, 79, 82, 84, 87, 88, 91, 94, 97, 99, 101, 102],
[9, 13, 16, 19, 21, 23, 25, 29, 31, 35, 38, 39, 42, 45, 48, 51, 54, 56, 57, 60, 63, 64, 67, 69, 72, 73, 74, 76, 79, 81, 85, 88, 90, 92, 95, 98, 100, 101, 104, 106],
[10, 16, 19, 22, 24, 26, 29, 31, 34, 36, 40, 41, 45, 46, 50, 54, 56, 59, 60, 63, 66, 69, 70, 72, 75, 77, 79, 81, 83, 85, 88, 91, 93, 96, 98, 99, 102, 105, 107, 109],
[12, 18, 22, 25, 26, 29, 32, 33, 37, 39, 42, 44, 47, 50, 52, 57, 59, 61, 62, 66, 68, 71, 72, 74, 76, 80, 82, 84, 87, 90, 92, 94, 95, 98, 101, 102, 105, 107, 109, 112]]
print(array)
task1=Solution()
t=task1.Find(22,array)
print(t)