lintcode-search two-dimensional matrix Java

Source: Internet
Author: User
Tags lintcode

Title Description:

Write an efficient algorithm to search for values in the m x n Matrix.

This matrix has the following characteristics:

    • The integers in each row are sorted from left to right.
    • The first number of each row is greater than the last integer on the previous line.

Code implementation:

 Public classSolution {/** @param Matrix:matrix, a list of lists of integers * @param target:an integer * @return: A Boolean, in Dicate whether matrix contains target*/     Public BooleanSearchmatrix (int[] Matrix,inttarget) {        if(Matrix = =NULL|| Matrix.length==0){            return false; }        if(matrix[0]==NULL|| Matrix[0].length==0){            return false; }        introw =matrix.length; intCol = matrix[0].length; intStart = 0; intend = row * COL-1;  while(Start <=end)                        {System.out.println (start); intMid = (end + start)/2; intnum = matrix[mid/col][mid%Col]; if(num==target) {                return true; }Else if(Num >target) {End= Mid-1; }Else{Start= Mid+1; }        }        return false; }}

Remark: A problem that pits me,

1. Two-point lookup, where mid has two representations, one (End+start)/2, one start+ (End-start)/2

2. Know the index to seek the elements of the matrix: using index/col column elements;

lintcode-search two-dimensional matrix Java

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.