Lintcode Medium Title: Search a 2d Matrix II searching for two-dimensional matrices II

Source: Internet
Author: User
Tags lintcode

Topic

search for two-dimensional Matrix II

Write an efficient algorithm to search for values in the MXN matrix, returning the number of occurrences of this value.

This matrix has the following characteristics:

    • The integers in each row are sorted from left to right.
    • The integers of each column are sorted from top to bottom.
    • There are no duplicate integers in each row or column.
Sample Example

Consider the following matrices:

[

[1, 3, 5, 7],

[2, 4, 7, 8],

[3, 5, 9, 10]

]

Give target = 3, return 2

challenges

Requires O (m+n) time complexity and O (1) Extra space

Solving

Direct traversal with a time complexity of O (MN)

 Public classSolution {/**     * @parammatrix:a List of lists of integers *@param: A number you want to search in the matrix *@return: An integer indicate the occurrence of target in the given matrix*/     Public intSearchmatrix (int[] Matrix,inttarget) {        //Write your code here        if(Matrix = =NULL)            return0; introw =matrix.length; if(Row ==0)            return0; intCol = matrix[0].length; intCount =0;  for(inti=0;i< Row; i++){             for(intj=0;j<col;j++){                if(Matrix[i][j] = =target) Count++; }        }        returncount; }}
Java Code

The array in question is ordered, and the elements of each row or column are not duplicated.

It can be found that the number of occurrences occurs between 0 and min (col,row)

There seems to be a very similar question on the offer.

By dividing the matrix, go one column at a time

Java programs

 Public classSolution {/**     * @parammatrix:a List of lists of integers *@param: A number you want to search in the matrix *@return: An integer indicate the occurrence of target in the given matrix*/     Public intSearchmatrix (int[] Matrix,inttarget) {        //Write your code here        if(Matrix = =NULL)            return0; introw =matrix.length; if(Row ==0)            return0; intCol = matrix[0].length; intCount =0; intI=0; intJ=col-1;  while(I<row && j>=0){            if(Matrix[i][j] >target) {J--; }Else if(matrix[i][j]<target) {i++; }Else{Count++; I++; J--; }        }        returncount; }    }
Java Code

Python program

classSolution:"""@param matrix:an List of lists of integers @param target:an integer you want to search in matrix @return: An integer indicates the total occurrence of target in the given matrix"""    defSearchmatrix (self, Matrix, target):#Write your code here        ifMatrix = =None:return0 Row=len (Matrix)ifrow = =0:return0 Col=Len (matrix[0]) Count=0 I=0 J= Col-1 whileI<row andj>=0:ifMATRIX[I][J] >target:j-=1elifMATRIX[I][J] <target:i+ = 1Else: Count+ = 1I+ = 1J-= 1returnCount
Python Code

Lintcode Medium Title: Search a 2d Matrix II searching for two-dimensional matrices II

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.