{"id":168,"date":"2017-09-09T01:39:47","date_gmt":"2017-09-09T08:39:47","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=168"},"modified":"2018-07-05T16:16:14","modified_gmt":"2018-07-05T23:16:14","slug":"leetcode-204-count-primes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-204-count-primes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 204. Count Primes"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"LeetCode 204. Count Primes - \u82b1\u82b1\u9171 \u5237\u9898\u627e\u5de5\u4f5c EP42\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/Kwo2jkHOyPY?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p><strong>Problem:<\/strong><\/p>\n<p>Count the number of prime numbers less than a non-negative number,\u00a0<b><i>n<\/i><\/b>.<\/p>\n<p><strong>Idea:<\/strong><\/p>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Sieve_of_Eratosthenes\">Sieve of Eratosthenes<\/a><\/p>\n<p><strong>Time Complexity:<\/strong><\/p>\n<p>O(nloglogn)<\/p>\n<p><strong>Space Complexity:<\/strong><\/p>\n<p>O(n)<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\nclass Solution {\r\npublic:\r\n    int countPrimes(int n) {\r\n        if (n &lt; 3) return 0;\r\n        \r\n        vector&lt;unsigned char&gt; f(n, 1);\r\n        f[0] = f[1] = 0;\r\n        for(long i=2;i&lt;=sqrt(n);++i) {\r\n            if(!f[i]) continue;\r\n            for(long j=i*i;j&lt;n;j+=i)\r\n                f[j] = 0;\r\n        }\r\n        \r\n        int ans = accumulate(f.begin(), f.end(), 0);\r\n        return ans;\r\n    }\r\n};<\/pre>\n<p>Java<\/p>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 16 ms\r\nclass Solution {\r\n  public int countPrimes(int n) {\r\n    int ans = 0;\r\n    boolean[] isPrime = new boolean[n + 1];\r\n    Arrays.fill(isPrime, true);\r\n    isPrime[0] = false;\r\n    if (n &gt; 0) isPrime[1] = false;\r\n    for (int i = 2; i &lt; n; ++i) {\r\n      if (!isPrime[i]) continue;\r\n      ++ans;\r\n      for (int j = 2 * i; j &lt; n; j += i)\r\n        isPrime[j] = false;\r\n    }\r\n    return ans;\r\n  }\r\n}<\/pre>\n<p>Python3<\/p>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 800 ms\r\n\"\"\"\r\nclass Solution:\r\n  def countPrimes(self, n):\r\n    if n &lt; 3: return 0\r\n    isPrime = [True] * (n + 1)\r\n    isPrime[0] = False\r\n    isPrime[1] = False\r\n    ans = 0\r\n    for i in range(2, n):\r\n      if not isPrime[i]: continue\r\n      ans += 1\r\n      for j in range(2 * i, n, i):\r\n        isPrime[j] = False\r\n    return ans\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: Count the number of prime numbers less than a non-negative number,\u00a0n. Idea: Sieve of Eratosthenes Time Complexity: O(nloglogn) Space Complexity: O(n) Solution: C++ \/\/&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,59,60,61],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-prime","tag-sieve","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/168","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=168"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":3003,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/168\/revisions\/3003"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}