跳至主要内容

204. 计数质数

class Solution {
public:
    int countPrimes(int n)
    {
        if (n <= 1)
        {
            return 0;
        }

        bool * isPrime = new bool[n + 1];
        for (int i = 0; i < n + 1; i++)
        {
            isPrime[i] = true;
        }

        vector<int> prime;

        int res = 0;
        for (int i = 2; i < n; i++)
        {
            if (isPrime[i])
            {
                prime.push_back(i);
            }
            for (int j = 0; j < prime.size() && i * prime[j] < n; j++)
            {
                isPrime[i * prime[j]] = false;
                if (i % prime[j] == 0)
                {
                    break;
                }
            }
        }

        return prime.size();
    }
};

评论

此博客中的热门博文

ubuntu 添加root登录

Login to your server as root. As the root user, edit the sshd_config file found in  /etc/ssh/sshd_config : vim /etc/ssh/sshd_config ( For details on working with Vim check out our article here !) Add the following line to the file, you can add it anywhere but it’s good practice to find the block about authentication and add it there. PermitRootLogin yes Save and exit the file. Restart the SSH server: systemctl restart sshd or service sshd restart