[Leetcode questions and Notes] search a 2D matrix

Source: Internet
Author: User

Write an efficient algorithm that searches for a value inMXNMatrix. This matrix has the following properties:

 

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

 

For example,

Consider the following matrix:

[  [1,   3,  5,  7],  [10, 11, 16, 20],  [23, 30, 34, 50]]

GivenTarget=3, Returntrue.

 

Question: each time we use the elements in the upper right corner to compare them with the target, there are three situations:

  1. Equal, meaning target is found;
  2. If the element in the upper-right corner is larger than the target element, it indicates that the target is in the first row and the first row is searched recursively.
  3. If the element in the upper-right corner is smaller than the target element, it indicates that the target is located in the second row and later. recursive search is shown in the following area:

The Code is as follows:

 1 public class Solution { 2     private boolean IfFind = false; 3     private void RightCornerRecur(int[][] matrix,int target,int m_start,int m_end,int n_start,int n_end){ 4         if(m_start > m_end || n_start > n_end) 5             return; 6              7         if(m_start < 0 || n_start < 0 || m_end >= matrix.length || n_end >= matrix[0].length) 8             return; 9         10         if(matrix[m_start][n_end] == target){11             IfFind = true;12             return;13         }14         if(matrix[m_start][n_end] > target)15             RightCornerRecur(matrix, target, m_start, m_start, n_start, n_end-1);16         else {17             RightCornerRecur(matrix, target, m_start+1,m_end, n_start, n_end);18         }19 20     }21     public boolean searchMatrix(int[][] matrix, int target) {22         RightCornerRecur(matrix, target,0,matrix.length-1, 0, matrix[0].length-1);23         return IfFind;24     }25 }

The upper-right corner can be used to compare and cut off some elements, and the lower-left corner can also be used. The following Code adds a comparison between the lower-left element and the target element, and the final running time is 384 Ms, in this case, the running time in the upper-right corner is 472 Ms.

 1 public class Solution { 2     private boolean IfFind = false; 3     private void RightCornerRecur(int[][] matrix,int target,int m_start,int m_end,int n_start,int n_end){ 4          5         if(m_start > m_end || n_start > n_end) 6             return; 7              8         if(m_start < 0 || n_start < 0 || m_end >= matrix.length || n_end >= matrix[0].length) 9             return;10         11     12         if(matrix[m_start][n_end] == target){13             IfFind = true;14             return;15         }16         17         if(matrix[m_start][n_end] > target)18             RightCornerRecur(matrix, target, m_start, m_start, n_start, n_end-1);19         20         else {21             if(matrix[m_end][0] == target){22                 IfFind = true;23                 return;24             }25             if(matrix[m_end][0] < target)26                 RightCornerRecur(matrix, target, m_end, m_end, n_start+1, n_end);27             else28                 RightCornerRecur(matrix, target, m_start+1,m_end-1, n_start, n_end);29         }30 31     }32     public boolean searchMatrix(int[][] matrix, int target) {33         RightCornerRecur(matrix, target,0,matrix.length-1, 0, matrix[0].length-1);34         return IfFind;35     }36 }

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.