Leetcode first click Search a 2D Matrix

Source: Internet
Author: User

Question.

I didn't come up with any essential difference between storing data in a matrix. The conditions given by the question determine that each row is spliced in sequence and the final result is an ordered array, therefore, binary can be used directly in the entire matrix, except that the specific data can be calculated with the/and % values each time.

Class Solution {public: bool bsearch (vector <int> & metrix, int target, int n, int start, int end) {if (start> end) return false; int mid = (start + end)/2; int I = mid/n, j = mid % n; if (metrix [I] [j] = target) return true; if (metrix [I] [j]> target) return bsearch (metrix, target, n, start, mid-1); else return bsearch (metrix, target, n, mid + 1, end);} bool searchMatrix (vector <int> & matrix, int target) {int m = matrix. size (), n = matrix [0]. size (); return bsearch (matrix, target, n, 0, m * N-1 );}};


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.