"Sword Point offer" Java implementation

Source: Internet
Author: User

Interview questions Find in 32-D arrays leetcode--74 search a 2D Matrix

1 /*Java2 Write An efficient algorithm, searches for a value in a m x n matrix. This matrix has the following properties:3 4 integers in each row is sorted from the left to the right.5 The first integer of each row was greater than the last integer of the previous row.6 7 For example,8 9 consider the following matrix:Ten  One [ A [1, 3, 5, 7], - [Ten, one, +,], - [ Max., the ] -  - Given target = 3, return true. - the simplest way to do this is to iterate through rows and columns but with a high degree of time complexity.  + */ - classSolution { +      Public BooleanSearchmatrix (int[] Matrix,inttarget) { A         BooleanFind =false; at         intRows,cols; -         if(Matrix = =NULL|| Matrix.length==0) { -rows = 0; -cols = 0; -}Else { -rows =matrix.length; incols = matrix[0].length; -         } to              +         if(Matrix! =NULL&& rows > 0 && cols > 0) { -             intRow = 0; the             intCol = cols-1; *              while(Row<rows && col>=0){ $                 if(matrix[row][col]==target) {Panax NotoginsengFind=true; -                      Break; the                 } +                      A                 Else if(matrix[row][col]>target) { thecol--; +}Else{ -row++; $                 } $             } -         } -  the         returnfind; -     }Wuyi  Public Static voidMain (string[] args) { the         int[] matrix1 = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 50 } }; -  Wu         int[] Matrix = {}; -         inttarget = 55; AboutSearcha2dmatrix cc =NewSearcha2dmatrix (); $         BooleanFind =Cc.searchmatrix (matrix, target); - System.out.println (find); -     } -}
View Code

"Sword Point offer" Java implementation

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.