p204 发表于 2017-02-16 | 分类于 blog | LeetCode p204 Count Primes 题解1.题目: Description: Count the number of prime numbers less than a non-negative number, n. 题意: 输入n计算小于n的数中有多少个质数。 2.解题思路: 用数组进行储存,见代码。 3.代码 [title] [] [url] [link text]12345678910111213141516 public class Solution { public int countPrimes(int n) { int ans = 0; boolean p[] = new boolean[n+1]; for (int i = 2; i < n; i++) { if (p[i] == false) { ans++; for (int j = 2; i * j < n; j++) { p[i * j] = true; } } } return ans; }} 4.一些总结: