본문 바로가기

Problem Solving/리트코드(leetcode)

Count Primes

class Solution {
public:
    int countPrimes(int n) {
        bool arr[5000001];
        fill_n(arr, 5000001, 1);
        int count = 0;
        for(int i = 2; i < n; i++) {
            if(arr[i] == true) count++;
            else continue;
            for(int j = 2; i*j < n; j++) {
                arr[i*j] = false;
            }
        }
        
        return count;
    }
};

'Problem Solving > 리트코드(leetcode)' 카테고리의 다른 글

Summary Ranges  (0) 2022.01.16
Single Number  (0) 2022.01.16
[Leetcode/C++] 66. Plus One  (0) 2022.01.16
Pascal's Triangle  (0) 2022.01.16
[Leetcode/C++] 455. Assign Cookies  (0) 2022.01.16