본문 바로가기
Leetcode

[LeetCode] Count Primes 파이썬

by YGSEO 2021. 4. 4.
728x90
class Solution:
    def countPrimes(self, n: int) -> int:
        return len(self.getPrimaryNum_Eratos(n))
    
    def getPrimaryNum_Eratos(self,N): 
        nums = [True] * (N + 1) 
        for i in range(2, len(nums) // 2 + 1): 
            if nums[i] == True: 
                for j in range(i+i, N, i): 
                    nums[j] = False 
        return [i for i in range(2, N) if nums[i] == True]

 

 

출처: somjang.tistory.com/entry/leetCode-204-Count-Primes-Python

728x90

댓글