[LeetCode]204. Count Primes

xiaoxiao2021-02-28  97

Description:

Count the number of prime numbers less than a non-negative number, n.

public class Solution { public int countPrimes(int n) { int res=0; boolean[] prime = new boolean[n]; //false:prime; true:prime for(int i=2; i<n; i++){ if(!prime[i]){ res++; for(int j=2;j*i<n; j++) prime[j*i]=true; } } return res; } }

转载请注明原文地址: https://www.6miu.com/read-60454.html

最新回复(0)