{"id":3093,"date":"2020-02-28T15:35:01","date_gmt":"2020-02-28T07:35:01","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=3093"},"modified":"2020-02-28T15:35:01","modified_gmt":"2020-02-28T07:35:01","slug":"%e3%80%90leetcode%e3%80%910074-%e6%90%9c%e7%b4%a2%e4%ba%8c%e7%bb%b4%e7%9f%a9%e9%98%b5","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/algorithm\/3093\/","title":{"rendered":"\u3010LeetCode\u30110074 \u641c\u7d22\u4e8c\u7ef4\u77e9\u9635"},"content":{"rendered":"\n<p><a href=\"https:\/\/leetcode-cn.com\/problems\/search-a-2d-matrix\/\">https:\/\/leetcode-cn.com\/problems\/search-a-2d-matrix\/<\/a><\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-\n<em>\n<\/em>class Solution(object):\n    def searchMatrix(self, matrix, target):\n        <em>\"\"\"\n        <\/em><strong><em>:type<\/em><\/strong><em> matrix: List[List[int]]\n        <\/em><strong><em>:type<\/em><\/strong><em> target: int\n        <\/em><strong><em>:rtype<\/em><\/strong><em>: bool\n        \"\"\"\n        <\/em>if len(matrix) == 0 or len(matrix[0]) == 0:\n            return False\n        target_list_idx = 0\n        for i in range(len(matrix)):\n            if matrix[i][0] > target:\n                break\n            target_list_idx = i\n        target_list_len = len(matrix[target_list_idx])\n        left = 0\n        right = target_list_len - 1\n        while left &lt;= right:\n            if left &lt; 0 or right > target_list_len - 1:\n                return False\n            mid = int((left + right) \/ 2)\n            nub = matrix[target_list_idx][mid]\n            if nub == target:\n                return True\n            elif nub &lt; target:\n                left = mid + 1\n            else:\n                right = mid - 1\n        return False\nif __name__ == '__main__':\n    matrix = [\n        [1, 3, 5, 7],\n        [10, 11, 16, 20],\n        [23, 30, 34, 50]\n    ]\n    target =3\n    matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]\n    # print(len(matrix))\n    # print(len(matrix[0]))\n    print(Solution().searchMatrix(matrix, target))\n<\/pre>\n\n\n<p>\u6211\u53c8\u5f00\u59cb\u4e86\u6211\u53c8\u5f00\u59cb\u4e86\u6211\u53c8\u5f00\u59cb\u4e86<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"113\" src=\"\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-119.png\" alt=\"\" class=\"wp-image-3094\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-119.png 483w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/02\/\u56fe\u7247-119-300x70.png 300w\" sizes=\"(max-width: 483px) 100vw, 483px\" \/><\/figure><\/div>\n\n\n<p>\u601d\u8def\uff1a\u5148\u641c\u7d22\u6bcf\u4e00\u884c\u76840\u53f7\u5143\u7d20\uff0c\u7136\u540e\u5728\u5217\u8868\u4e2d\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u3002<\/p>\n\n\n<p>\u6211\u4f30\u8ba1\u6211\u65f6\u95f4\u590d\u6742\u5ea6\u8fd9\u4e48\u4f4e\u7684\u539f\u56e0\u662f\u597d\u591a\u4eba\u6ca1\u4e8c\u5206\u5427\u54c8\u54c8\u54c8\u6361\u4e2a\u6f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/leetcode-cn.com\/problems\/search-a-2d-matri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[10],"tags":[],"views":2474,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3093"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=3093"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/3093\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=3093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=3093"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=3093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}