Find a two-dimensional array - offer the first question to prove safety

 

Subject description:

  • In a two-dimensional array (the same as the length of each one-dimensional array),
  • Each row from left to right in order of increasing sort,
  • Each column sorted in ascending order from top to bottom.
  • A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Design ideas:

 

  1. Over lines
  2.       Through the rows each element
  3.             Analyzing element value is equal to the given target value
  4.                       Equal returns True
  5. Traversed did not return false

Code:

         

. 1  Import numpy AS NP
 2  class Solution:
 . 3      # Array two-dimensional list 
. 4      DEF the Find (Self, target, Array):
 . 5          # through each row 
. 6          for I in Array:
 . 7              # through each element of each row 
. 8              for J in I:
 . 9                  IF J == target:
 10                      return True
 . 11          # are not, then return False 
12 is          return False
 13 is List = [[l, 2,3], [4,5,6], [7,8,9 ]]
 14 Array = np.array(list)
15 target = int(input())
16 s = Solution()
17 judge = s.Find(target,array)
18 print(judge)

 

Stuck problem:

    Keyboard input problems can not be solved a long time two-dimensional array

 

 solve:

 

Guess you like

Origin www.cnblogs.com/carol-main-blog/p/11516681.html