{"id":6474,"date":"2020-03-13T23:35:32","date_gmt":"2020-03-14T06:35:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6474"},"modified":"2020-03-13T23:37:16","modified_gmt":"2020-03-14T06:37:16","slug":"leetcode-240-search-a-2d-matrix-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/two-pointers\/leetcode-240-search-a-2d-matrix-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 240. Search a 2D Matrix II"},"content":{"rendered":"\n<p>Write an efficient algorithm that searches for a value in an&nbsp;<em>m<\/em>&nbsp;x&nbsp;<em>n<\/em>&nbsp;matrix. This matrix has the following properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Integers in each row are sorted in ascending from left to right.<\/li><li>Integers in each column are sorted in ascending from top to bottom.<\/li><\/ul>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<p>Consider the following matrix:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">[\n  [1,   4,  7, 11, 15],\n  [2,   5,  8, 12, 19],\n  [3,   6,  9, 16, 22],\n  [10, 13, 14, 17, 24],\n  [18, 21, 23, 26, 30]\n]\n<\/pre>\n\n\n\n<p>Given&nbsp;target&nbsp;=&nbsp;<code>5<\/code>, return&nbsp;<code>true<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Two Pointers<\/strong><\/h2>\n\n\n\n<p>Start from first row + last column, if the current value is larger than target, &#8211;column; if smaller then ++row.<\/p>\n\n\n\n<p>e.g. <br>1. r = 0, c = 4, v = 15, 15 &gt; 5 =&gt; &#8211;c<br>2. r = 0, c = 3, v = 11, 11 &gt; 5 =&gt; &#8211;c<br>3. r = 0, c = 2, v = 7, 7 &gt; 5 =&gt; &#8211;c<br>4. r = 0, c = 1, v = 4, 4 &lt; 5 =&gt; ++r<br>5. r = 1, c = 1, v = 5, 5 = 5, found it!<\/p>\n\n\n\n<p>Time complexity: O(m + n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool searchMatrix(vector<vector<int>>& matrix, int target) {\n    if (matrix.empty() || matrix[0].empty()) return false;\n    const int m = matrix.size();\n    const int n = matrix[0].size();\n    int r = 0;\n    int c = matrix[0].size() - 1;\n    while (r < m &#038;&#038; c >= 0) {\n      if (matrix[r][c] == target) return true;\n      else if (matrix[r][c] > target) --c;\n      else ++r;\n    }\n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write an efficient algorithm that searches for a value in an&nbsp;m&nbsp;x&nbsp;n&nbsp;matrix. This matrix has the following properties: Integers in each row are sorted in ascending&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184,176],"tags":[52,216,177,175],"class_list":["post-6474","post","type-post","status-publish","format-standard","hentry","category-array","category-two-pointers","tag-binary-search","tag-matrix","tag-medium","tag-two-pointers","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6474"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6474\/revisions"}],"predecessor-version":[{"id":6476,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6474\/revisions\/6476"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}