首页 > 其他 > 详细

#Leetcode# 204. Count Primes

时间:2018-11-15 14:27:41      阅读:128      评论:0      收藏:0      [点我收藏+]

https://leetcode.com/problems/count-primes/

 

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

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

代码:

class Solution {
public:
    int countPrimes(int n) {
        int ans = 0;
        if(n == 0 || n == 1)
            return 0;
        for(int i = 2; i < n; i ++) 
            if(isPrime(i)) ans ++;
        
        return ans;
    }
    bool isPrime(int x) {
        for(int i = 2; i * i <= x; i ++) 
            if(x % i == 0) return false;
        return true;
    }
};

  

#Leetcode# 204. Count Primes

原文:https://www.cnblogs.com/zlrrrr/p/9963197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!