(LeetCode74) Search a 2D Matrix

Source: Internet
Author: User

Write an efficient algorithm, searches for a value in a m x n Matrix. This matrix has the following properties:

    • Integers in each row is sorted from the left to the right.
    • The first integer of each row was greater than the last integer of the previous row.

For example,

Consider the following matrix:

[  [1,   3,  5,  7],  [Ten, One,],  [23, 30, 34, 50]]

Given Target = 3 , return true .

Topic:

In a m*n two-dimensional array, each row is incremented from left to right, and the first element of each row is larger than the last element in the previous row.

Determines whether an element is in the array.

Ideas:

One implication of the problem is that each column is incremented from top to bottom.

Method 1:

If you expand a two-dimensional array into rows, it is a sorted one-dimensional array, so it is easy to find the answer by looking at the binary of a one-dimensional array.

Method 2:

Given the regularity of the array, select the upper-right number of the array look-up range, if it is equal to the number found, returns true if the number is larger than the lookup, and if it is smaller than the lookup number, the row of the number is excluded from the lookup range.

Method 3:

Finds the row of the element first by two points, and then finds the element by two points in the same row.

Code:

1, one-dimensional array method:

classSolution { Public:    BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) {        intRows=matrix.size (); intcols=matrix[0].size (); intleft=0, right= (rows*cols-1); intMid,r,c,val;  while(left<=Right ) {Mid=left+ (Right-left) >>1); R=mid/cols; C=mid%cols; if(matrix[r][c]==target)return true; if(matrix[r][c]<target) left=mid+1; Else Right=mid-1; }        return false; }};

2. Upper right corner element comparison method

classSolution { Public:    BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) {        intRows=matrix.size (); intcols=matrix[0].size (); intR=0, c=cols-1;  while(R<rows && c>=0){            if(target==Matrix[r][c])return true; if(target<Matrix[r][c]) C--; ElseR++; }        return false; }};

3, two-point lookup line method

classSolution { Public:    BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) {        intRows=matrix.size (); intcols=matrix[0].size (); intlrow=0, rrow=rows-1; intMidrow;  while(lrow<=Rrow) {Midrow=lrow+ (Rrow-lrow) >>1); if(matrix[midrow][0]==target | | matrix[midrow][cols-1]==target)return true; if(matrix[midrow][0]<target && matrix[midrow][cols-1]>target) Break; if(matrix[midrow][0]>target) Rrow=midrow-1; ElseLrow=midrow+1; }                if(lrow<=Rrow) {            intLcol=0, rcol=cols-1; intMidcol;  while(lcol<=Rcol) {Midcol=lcol+ (Rcol-lcol) >>1); if(matrix[midrow][midcol]==target)return true; if(matrix[midrow][midcol]>target) Rcol=midcol-1; ElseLCol=midcol+1; }        }                return false; }};

(LeetCode74) Search a 2D Matrix

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.