Leetcode Search a 2D Matrix II

Source: Internet
Author: User

The original title link is here: https://leetcode.com/problems/search-a-2d-matrix-ii/

The search a 2D matrix is very similar to the title, but the content is very different.

The point is that each row of each column is ascending, starting at the top right corner x = matrix[i][j], i = 0, j = matrix[0].length-1, and with X and target, if equal, returns True.

If it is less than target, the description is definitely not in this line, so i++. If it is greater than target, the description must not be in this column so J--.

If the walk to the boundary has not returned stating that there is no target, return false.

The initial amount of note:j is J = n-1, note minus one.

AC Java:

1  Public classSolution {2      Public BooleanSearchmatrix (int[] Matrix,inttarget) {3         if(Matrix = =NULL|| Matrix.length = = 0 | | Matrix[0].length = = 0){4             return false;5         }6         intm =matrix.length;7         intn = matrix[0].length;8         inti = 0;9         intj = n-1;//ErrorTen          while(I<m && j>=0){ One             intx =Matrix[i][j]; A             if(x = =target) { -                 return true; -}Else if(X <target) { thei++; -}Else{ -j--; -             } +         } -         return false; +     } A}

Leetcode Search a 2D Matrix II

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.