【Leetcode】Count Primes

来源:互联网 发布:js 汉字长度 编辑:程序博客网 时间:2024/05/23 14:26

题目链接:https://leetcode.com/problems/count-primes/

题目:

Description:

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

思路:

埃拉托色尼选筛法

算法:

public int countPrimes(int n) {          boolean c[] = new boolean[n];          for (int i = 2; i * i < n; i++) {              if(!c[i]){                  for (int j = i+i; j < n; j += i) {                      if(!c[j])                          c[j] = true;                  }              }          }          int count = 0;          for (int i = 2; i < n; i++) {              if (!c[i])                  count++;          }          return count;      }  


1 0
原创粉丝点击