[LeetCode-interview algorithm classic-Java implementation] [074-Search a 2D Matrix (Search 2D Matrix)], leetcode -- java

Source: Internet
Author: User

[LeetCode-interview algorithm classic-Java implementation] [074-Search a 2D Matrix (Search 2D Matrix)], leetcode -- java
[074-Search a 2D Matrix (Search 2D Matrix )][LeetCode-interview algorithm classic-Java implementation] [directory indexes for all questions]Original question

Write an efficient algorithm that searches for a value in an m x n matrix. 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: Given target = 3, return true.

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

Theme

Given a two-dimensional matrix, an algorithm can be quickly searched in the matrix. That is, if k is specified, search k in the matrix.
Properties in the bottom of the matrix: each row and column are sorted, and the first number of each row is larger than the last number of the previous row.

Solutions

Solution 1: first use the binary query algorithm to find the column where the number is located, and then use the binary search algorithm to find the column where the number is located. If it is found, true is returned. Otherwise, false is returned. Solution 2: see [offoffoffoffer learning] [interview question 3: search in a two-dimensional array]

Code Implementation

Algorithm Implementation class

Public class Solution {public boolean searchMatrix (int [] [] matrix, int target) {if (matrix = null | matrix. length = 0 | matrix [0]. length = 0) {return false;} int row = matrix. length; int column = matrix [0]. length; int low = 0; int high = row-1; int mid = 0; // find the column where the result is located while (low <= high) {mid = low + (high-low)/2; if (target <matrix [mid] [column-1]) {high = mid-1 ;} else if (target> matrix [mid] [column-1]) {low = mid + 1;} else {return true ;}} // determines the final position of the column. int targetRow = mid; if (matrix [mid] [column-1] <target) {targetRow ++;} // The target column exceeds, no result if (targetRow> = row) {return false;} low = 0; high = column-1; // find the row and return true, false while (low <= high) {mid = low + (high-low)/2; if (target <matrix [targetRow] [mid]) {high = mid-1;} else if (target> matrix [targetRow] [mid]) {low = mid + 1;} else {return true ;}} return false ;}}
Evaluation Result

  Click the image. If you do not release the image, drag it to a position. After the image is released, you can view the complete image in the new window.

Note Please refer to the source for reprinting at http://blog.csdn.net/derrantcm/article/details/47142931]

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.