[Leetcode] Search a 2D Matrix II searches for two-dimensional matrices

Source: Internet
Author: User
Tags lintcode

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 in ascending from left to right.
    • Integers in each column is sorted in ascending from top to bottom.

For example,

Consider the following matrix:

[  [1,   4,  7, one, a],  [2,   5, 8,, +  ],  [3,   6,  9, +, +],  [10, 13, 14, 17,  [18, 21, 23, 26, 30]

Given Target = 5 , return true .

Given Target = 20 , return false .

Suddenly found Leetcode very fond of stealing from the Lintcode, this is forcing me to brush lintcode rhythm?! This problem let us in a two-dimensional array of fast search for a number, the two-dimensional array of rows of the columns are in ascending order, is the search a 2D matrix of a two-dimensional matrix, the difference is that the first number of each row is larger than the last digit of the previous line, is an array of integral serpentine increments. So the problem is that you can expand a two-dimensional array into an array using a two-check search. And this problem can not do that, the problem has its own characteristics. If we look at the example given in the topic, we can see that there are two locations where the number is very characteristic, the lower left and the upper right corner. The lower left 18, all the number to a small, to the right of all the number increase, then we can compare with the target number, if the target number is large, go to the right, if the target number is small, go to the left search. This makes it possible to determine whether the target number exists. Of course, we can also put the starting number in the upper right corner, left and next search, the stop condition is set correctly on the line. The code is as follows:

classSolution { Public:    BOOLSearchmatrix (vector<vector<int> > &matrix,inttarget) {        if(Matrix.empty () | | matrix[0].empty ())return false; if(Target < matrix[0][0] || Target > Matrix.back (). Back ())return false; intx = Matrix.size ()-1, y =0;  while(true) {            if(Matrix[x][y] > target)--x; Else if(Matrix[x][y] < target) + +y; Else return true; if(X <0|| Y >= matrix[0].size ()) Break; }        return false; }};

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Search a 2D Matrix II searches for two-dimensional matrices

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.