虽然Difficulty标为Easy,但对自己来说不算简单呀,犯了各种错误。
参考网上的各种代码,才终于跑通了。 算法其实就是经典的 Sieve of Eratosthenes,复杂度 O(NloglogN)
ref
Java
1
1 | class Solution { |
Submission Detail
- 20 / 20 test cases passed.
- Your runtime beats 42.53 % of java submissions.
2
这道题好像可以拿来作为范例学习Java 8 Streams:
- Java 8 Sieve of Eratosthenes
- Java 8: streams and the Sieve of Eratosthenes
- Sieve of Eratosthenes using Java 8 streams
- Generating Prime Numbers in Java
1 | import java.util.BitSet; |
Submission Detail
- 20 / 20 test cases passed.
- Your runtime beats 14.45 % of java submissions.
代码更精炼了,但性能变差了
3 bottom-up approach
solution of Leetcode 204. Count Primes
1 | class Solution { |
Submission Detail
- 20 / 20 test cases passed.
- Runtime: 13 ms, faster than 77.25% of Java online submissions for Count Primes.
- Memory Usage: 38.3 MB, less than 5.66% of Java online submissions for Count Primes.
JavaScript
1 | /** |
Submission Detail
- 20 / 20 test cases passed.
- Your runtime beats 97.46 % of javascript submissions.