leetcode 204 Count Primes

来源:互联网 发布:ubuntu输入密码后卡住 编辑:程序博客网 时间:2024/06/01 12:15

Description:

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

Credits:

Special thanks to @mithmatt for adding this problem and creating all test cases.


class Solution {public:    int countPrimes(int n) {        if(n < 2) return 0;vector<int> visited(n, 0);int cnt = 0;for(int i=2; i<n; i++) {if(visited[i]==1) continue;cnt += 1;for(int j=i+i; j < n; j+=i)visited[j] = 1;}return cnt;    }};


0 0
原创粉丝点击