[LeetCode] 74. Search a 2D Matrix, leetcode74.search

Source: Internet
Author: User

[LeetCode] 74. Search a 2D Matrix, leetcode74.search
[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:

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

Given target =3, Returntrue.

[Idea 1]

Young's matrix solution:

(1) If the number selected in the array is exactly the same as the number found, the query ends and returns true.

(2) If the number selected in the array is smaller than the number to be searched, the number to be searched can only be on the right of the selected number according to the sorting rule. The number of columns is reduced by one.

(3) If the number selected in the array is greater than the number to be searched, the number to be searched can only be under the selected number according to the sorting rule. Add one to the number of rows.

Time Complexity: O (m + n)

[Code]
/* ------------------------------------ * Date: 2015-02-04 * Author: SJF0115 * Title: 74. search a 2D Matrix * URL: https://oj.leetcode.com/problems/search-a-2d-matrix/ * result: AC * Source: LeetCode * blog: ------------------------------------- */# include <iostream> # include <vector> # include <cstring> # include <algorithm> using namespace std; class Solution {public: bool searchMatrix (vector <int> & matrix, int target) {if (matrix. empty () {return false;} // if int row = matrix. size (); int col = matrix [0]. size (); int x = 0, y = col-1; // The Young's Matrix Solution starts from the element in the upper right corner while (x <row & y> = 0) {if (matrix [x] [y] = target) {return true;} // if else if (matrix [x] [y] <target) {++ x ;}// else {-- y ;}// while return false ;}}; int main () {Solution s; int target = 3; vector <int> matrix = {1, 3, 5, 7}, {10, 11, 16, 20}, {,}; bool result = s. searchMatrix (matrix, target); // output cout <result <endl; return 0 ;}



[Idea 2] According to the sorting rules, we can regard the matrix as a series of one-dimensional arrays. You can use binary search.

Convert the n * m matrix to a one-dimensional array: matrix [x] [y]-> a [x * m + y]

Convert a one-dimensional array to a matrix: a [x]-> matrix [x/m] [x % m]
Time Complexity: O (nmlognm) [Code 2]
/* ------------------------------------ * Date: 2015-02-04 * Author: SJF0115 * Title: 74. search a 2D Matrix * URL: https://oj.leetcode.com/problems/search-a-2d-matrix/ * result: AC * Source: LeetCode * blog: ----------------------------------------- */class Solution {public: bool searchMatrix (vector <int> & matrix, int target) {if (matrix. empty () {return false;} // if int row = matrix. size (); int col = matrix [0]. size (); int start = 0, end = row * col-1; // binary search solution int mid, x, y; while (start <= end) {mid = start + (end-start)/2; x = mid/col; y = mid % col; if (matrix [x] [y] = target) {return true;} // if else if (target> matrix [x] [y]) {start = mid + 1 ;} // else {end = mid-1;} // else} // while return false ;}};








Related Article

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.