Search in the two-dimensional array of "Sword Finger Offer"

Search in the two-dimensional array of "Sword Finger Offer"

I don't know where I am going, but I am already on my way!
Time is hurried, although I have never met, but I met Yusi, it is really a great fate, thank you for your visit!
  • Topic :
    In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer to determine whether the integer is contained in the array.
  • Example :
示例 1 :
输入:7,[[1,2,8,9],[2,4,9,12],[4,7,10,13],[6,8,11,15]]
返回值:true
  • Code 1:
# -*- coding:utf-8 -*-
class Solution:
    def Find(self, target, array):
        for i in range(len(array)):
            for j in range(len(array[0])):
                if target == array[i][j]:
                    return True
        return False
  • Algorithm description:
    traverse the array elements one by one, compare with the target value, if they are equal, return True, otherwise return False.

Guess you like

Origin blog.csdn.net/qq_34331113/article/details/114906432