{"id":773,"date":"2020-12-03T09:50:58","date_gmt":"2020-12-03T01:50:58","guid":{"rendered":"http:\/\/39.96.58.60\/?p=773"},"modified":"2022-10-18T16:37:28","modified_gmt":"2022-10-18T08:37:28","slug":"leetcode-204-%e8%ae%a1%e6%95%b0%e8%b4%a8%e6%95%b0","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=773","title":{"rendered":"Leetcode 204. \u8ba1\u6570\u8d28\u6570"},"content":{"rendered":"<p>\u7edf\u8ba1\u6240\u6709\u5c0f\u4e8e\u975e\u8d1f\u6574\u6570&nbsp;<em><code>n<\/code>&nbsp;<\/em>\u7684\u8d28\u6570\u7684\u6570\u91cf\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u793a\u4f8b 1\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 10\n\n<strong>\u8f93\u51fa\uff1a<\/strong>4\n\n<strong>\u89e3\u91ca\uff1a<\/strong>\u5c0f\u4e8e 10 \u7684\u8d28\u6570\u4e00\u5171\u6709 4 \u4e2a, \u5b83\u4eec\u662f 2, 3, 5, 7 \u3002\n\n<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 0\n\n<strong>\u8f93\u51fa\uff1a<\/strong>0\n\n<\/pre>\n<p><strong>\u793a\u4f8b 3\uff1a<\/strong><\/p>\n<pre><strong>\u8f93\u5165\uff1a<\/strong>n = 1\n\n<strong>\u8f93\u51fa<\/strong>\uff1a0\n\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>\u63d0\u793a\uff1a<\/strong><\/p>\n<ul>\n<li><code>0 &lt;= n &lt;= 5 * 10<sup>6<\/sup><\/code><\/li>\n<\/ul>\n<p>**\u96be\u5ea6**: Easy<\/p>\n<p>**\u6807\u7b7e**: \u54c8\u5e0c\u8868\u3001 \u6570\u5b66\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\uff1a104 ms, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e8695.17% \u7684\u7528\u6237\n\u5185\u5b58\u6d88\u8017\uff1a36.3 MB, \u5728\u6240\u6709 Python3 \u63d0\u4ea4\u4e2d\u51fb\u8d25\u4e866.94% \u7684\u7528\u6237\n\n\u89e3\u9898\u601d\u8def\uff1a\n    \u5384\u62c9\u591a\u585e\u7b5b\u6cd5\n    https:\/\/leetcode-cn.com\/problems\/count-primes\/solution\/pythonzui-you-jie-fa-mei-you-zhi-yi-liao-ba-by-bru\/\n\"\"\"\nclass Solution:\n    def countPrimes(self, n: int) -&gt; int:\n        if n &lt; 2:   # 1, 2 \u4e0d\u5b58\u5728\u8d28\u6570\n            return 0\n        record = [1] * n    # \u8bb0\u5f55\u8d28\u6570\uff0crecord[i]=1 \u8868\u793a\uff0c i+1\u662f\u8d28\u6570\n        record[0], record[1] = 0, 0  # 1, 2\u4e0d\u662f\u8d28\u6570\n        for i in range(2, int(n**0.5)+1):\n            if record[i]:\n                record[i*i:n:i] = [0]*((n-1-i*i)\/\/i+1)  # \u5982\u679c\u5f53\u524di\u662f\u8d28\u6570\uff0c\u52192*i\uff0c 3*i ... \u5747\u4e0d\u662f\u8d28\u6570\n        return sum(record)\n\n\n<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7edf\u8ba1\u6240\u6709\u5c0f\u4e8e\u975e\u8d1f\u6574\u6570&nbsp;n&nbsp;\u7684\u8d28\u6570\u7684\u6570\u91cf\u3002 &nbsp; \u793a\u4f8b 1\uff1a \u8f93\u5165\uff1an = 10 \u8f93&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/773"}],"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=773"}],"version-history":[{"count":1,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions"}],"predecessor-version":[{"id":774,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/773\/revisions\/774"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=773"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}