Two-dimensional array lookup

Source: Internet
Author: User

#Problem Description:
# in a two-dimensional array (each one-dimensional array is the same length), each row is ordered in ascending order from left to right.
# Each column is sorted in ascending order from top to bottom. Please complete a function, enter such a two-dimensional array and a
# Integer that determines whether the array contains the integer.

Class solution:    # Array Two-dimensional list    def Find (self, Target, array):        row = len (array)-1        #行数        col = Len (array[0 ])-1     #列数        i = 0        j = col while        j>=0 and I<=row:            if Array[i][j]>target:                j-= 1            elif Array[i][j]<target:                i+= 1            else:                return True        return False      matrix = [[1,2,3,4],[2,3,4,5], [3,4,5,6],[4,5,6,7]]s = Solution () print (S.find (5,matrix))

  

#Summary:
#思维关键点是矩阵左下角或者右上角, if the lower left corner, than its small, row 1 continues to find, if compared to the large, column +1 continues to find, the upper right corner is the opposite
#技术总结 two ways to define the ① of a two-dimensional array: List or Numpy.array ()
#②list can be loaded with different types of elements int float Str,array can only fit the same type
#③ get Element Method list[0][1],array[0,1]
#④list convert array to call the array method in the NumPy module Np.array (list)

Two-dimensional array lookup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.