标签:
Count Primes
问题:
Count the number of prime numbers less than a non-negative number, n
思路:
计算质数的方法:http://en.wikipedia.org/wiki/Prime_number
我的代码:
public class Solution { public int countPrimes(int n) { int count=0; boolean[] nums = new boolean[n]; for(int i=2; i<nums.length; i++){ if(!nums[i]){ count++; for(int j=2*i; j<nums.length; j=j+i){ nums[j] = true; } } } return count; } }
学习之处:
标签:
原文地址:http://www.cnblogs.com/sunshisonghit/p/4481380.html