二维数组中的查找 python

[编程题]二维数组中的查找

def choose(target,arr):
    rows=len(arr)-1
    cols=len(arr[0])-1
    i=rows
    j=0
    # while target<arr[i][0]:
    #     i-=1
    #     if i<0:
    #         print("out rows")
    #         return False 
    # while target!=arr[i][j]:
    #     j+=1
    #     if j>cols:
    #         print("out cols")
    #         return False 
    # print(i,j)
    # return True
    while i>=0 and j<=cols:
        if target<arr[i][j]:
            i-=1
        elif target>arr[i][j]:
            j+=1
        else:
            print(i,j)
            return  True
            
    return False


arr=[
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16]
]
result=choose(7,arr)

posted @ 2019-09-20 21:05  中庭之园  阅读(1330)  评论(0编辑  收藏  举报