【每日一题20220519】二维列表中的查找

:mage:‍ 在一个二维列表array中(每个一维列表的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。 请完成一个函数,输入这样的一个二维列表和一个整数,判断列表中是否含有该整数。 例如:存在一个二维列表:
[
[1,2,8,9],
[2,4,9,12],
[4,7,10,13],
[6,8,11,15]
]

【示例】
输入:7
输出:True
解释:数字7在[4,7,10,13]中出现。

题目难度:简单
题目来源:牛客网-二维列表中的查找

def solution(target: int, nums: list)-> bool:
    # your code here

assert solution(7, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is False
def solution(target: int, nums: list) -> bool:
    # your code here
    for i in nums:
        if target in i:
            return True
    else:
        return False


assert solution(7, [[1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15]]) is False

import numpy as np
def solution(target,nums):
a = np.array(nums)
print(target in a)
return (target in a)

assert solution(7, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is False

image


def solution(target: int, nums: list) -> bool:
    # your code here
    row = len(nums) - 1
    col = 0
    while col >= 0 and row >= 0:
        var = nums[row][col]
        if var == target:
            return True
        elif var < target:
            col += 1
        else:
            row -= 1
    return False


assert solution(7, [[1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1, 2, 8, 9], [2, 4, 9, 12], [4, 7, 10, 13], [6, 8, 11, 15]]) is False

def solution(target: int, nums: list)-> bool:
    for i in nums:
       if target in i:
           return True
    return False

assert solution(7, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is False
def solution(num,list):
    for i in range(len(list)):
        for j in range(len(list[i])):
            if num == list[i][j]:
                return True
    else:
        return False

assert solution(7, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is False
def solution(target: int, nums: list)-> bool:
    # your code here
    for num in nums:
        if target in num:
            return True
    else:
        return False


assert solution(7, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is True
assert solution(1, [[2]]) is False
assert solution(3, [[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]) is False
def solution(target: int, nums: list)-> bool:
    return any(target in i for i in nums)
关闭