{"id":661,"date":"2020-09-26T11:08:22","date_gmt":"2020-09-26T03:08:22","guid":{"rendered":"http:\/\/39.96.58.60\/?p=661"},"modified":"2022-10-18T16:38:24","modified_gmt":"2022-10-18T08:38:24","slug":"leetcode-861-%e7%bf%bb%e8%bd%ac%e7%9f%a9%e9%98%b5%e5%90%8e%e7%9a%84%e5%be%97%e5%88%86","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=661","title":{"rendered":"Leetcode 861. \u7ffb\u8f6c\u77e9\u9635\u540e\u7684\u5f97\u5206"},"content":{"rendered":"<p>\u6709\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635&nbsp;<code>A<\/code> \u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u4e3a&nbsp;<code>0<\/code>&nbsp;\u6216&nbsp;<code>1<\/code>&nbsp;\u3002<\/p>\n<p>\u79fb\u52a8\u662f\u6307\u9009\u62e9\u4efb\u4e00\u884c\u6216\u5217\uff0c\u5e76\u8f6c\u6362\u8be5\u884c\u6216\u5217\u4e2d\u7684\u6bcf\u4e00\u4e2a\u503c\uff1a\u5c06\u6240\u6709 <code>0<\/code> \u90fd\u66f4\u6539\u4e3a <code>1<\/code>\uff0c\u5c06\u6240\u6709 <code>1<\/code> \u90fd\u66f4\u6539\u4e3a <code>0<\/code>\u3002<\/p>\n<p>\u5728\u505a\u51fa\u4efb\u610f\u6b21\u6570\u7684\u79fb\u52a8\u540e\uff0c\u5c06\u8be5\u77e9\u9635\u7684\u6bcf\u4e00\u884c\u90fd\u6309\u7167\u4e8c\u8fdb\u5236\u6570\u6765\u89e3\u91ca\uff0c\u77e9\u9635\u7684\u5f97\u5206\u5c31\u662f\u8fd9\u4e9b\u6570\u5b57\u7684\u603b\u548c\u3002<\/p>\n<p>\u8fd4\u56de\u5c3d\u53ef\u80fd\u9ad8\u7684\u5206\u6570\u3002<\/p>\n<p>&nbsp;<\/p>\n<ol><\/ol>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>[[0,0,1,1],[1,0,1,0],[1,1,0,0]]\n\n<strong>\u8f93\u51fa\uff1a<\/strong>39\n\n<strong>\u89e3\u91ca\uff1a\n\n<\/strong>\u8f6c\u6362\u4e3a [[1,1,1,1],[1,0,0,1],[1,1,1,1]]\n\n0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ol>\n<li><code>1 &lt;= A.length &lt;= 20<\/code><\/li>\n<li><code>1 &lt;= A[0].length &lt;= 20<\/code><\/li>\n<li><code>A[i][j]<\/code>&nbsp;\u662f&nbsp;<code>0<\/code> \u6216&nbsp;<code>1<\/code><\/li>\n<\/ol>\n<p>**\u96be\u5ea6**: Medium<\/p>\n<p>**\u6807\u7b7e**: \u8d2a\u5fc3\u7b97\u6cd5\u3001<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism undefined-numbers lang-python\" data-lang=\"Python\"><code>\n# -*- coding: utf-8 -*-\n# @Author  : LG\n\n\"\"\"\n\u6267\u884c\u7528\u65f6\uff1a40 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8698.48% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a13.7 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e866.28% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u73b0\u5c06\u5bf9\u884c\u8fdb\u884c\u7ffb\u8f6c\uff0c\u5c06\u6bcf\u884c\u7b2c\u4e00\u4e2a\u53d8\u4e3a1\n    \u5176\u6b21\uff0c\u5bf9\u5217\u8fdb\u884c\u53d8\u6362\uff0c\u5982\u679c\u672c\u52170\u6570\u91cf\u8f83\u591a\uff0c\u5219\u7ffb\u8f6c\n\"\"\"\nclass Solution:\n    def matrixScore(self, A: List[List[int]]) -&gt; int:\n        m, n = len(A), len(A[0])\n        for i in range(m):  # \u6309\u884c\uff0c\u5982\u679c\u6bcf\u884c\u7b2c\u4e00\u4e2a\u4e3a0\uff0c\u5219\u7ffb\u8f6c\u8be5\u884c\n            if A[i][0] == 0:\n                A[i] = [1 if a == 0 else 0 for a in A[i]]\n\n        for j in range(n):  # \u6309\u5217\n            sum_ = 0\n            for i in range(m):\n                sum_ += A[i][j] # \u7edf\u8ba1\u672c\u884c\u975e0\u503c\n            if sum_ &lt; m\/2:  # \u5982\u679c\u975e0\u503c\u5c11\uff0c\u5219\u7ffb\u8f6c\u8be5\u5217\n                for i in range(m):\n                    A[i][j] = 0 if A[i][j] == 1 else 1\n        result = 0\n        for a in A:\n            result += int('0b'+''.join([str(aa) for aa in a]), base=2)\n        return result<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635&nbsp;A \u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\u4e3a&nbsp;0&nbsp;\u6216&nbsp;1&nbsp;\u3002 \u79fb\u52a8&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/661"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=661"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions"}],"predecessor-version":[{"id":662,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/661\/revisions\/662"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=661"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}