leetcode No204. Count Primes
来源:互联网 发布:mysql登录后修改密码 编辑:程序博客网 时间:2024/06/07 14:35
Question
Count the number of prime numbers less than a non-negative number, n.
Algorithm
筛法求素数
Accepted Code
python
class Solution(object): def countPrimes(self, n): if n<3: return 0 primes = [True]*n primes[0] = primes[1] = False for i in range(2,int(n ** 0.5)+1): # **代表乘方 if primes[i]: j = i*2; while(j < n): primes[j] = False j = j+i return sum(primes)
C++
class Solution {public: int countPrimes(int n) { if(n<3) return 0; vector<bool> v(n,true); for(int i=2;i<=sqrt(n);i++){ for(int j=i*2;j<n;j+=i){ v[j]=false; } } int res=0; for(int i=2;i<n;i++){ if(v[i]==true) res++; } return res; }};
阅读全文
0 0
- leetcode No204. Count Primes
- LeetCode 204 - Count Primes
- [LeetCode] Count Primes
- LeetCode #Count Primes#
- 【leetcode】Count Primes
- LeetCode 204-Count Primes
- [LeetCode] Count Primes
- [LeetCode 204] Count Primes
- [LeetCode]Count Primes
- leetcode 204: Count Primes
- Count Primes Leetcode JAVA
- LeetCode OJ Count Primes
- [leetcode]Count Primes
- leetcode--Count Primes
- [LeetCode]Count Primes
- Count Primes LeetCode
- LeetCode (36) Count Primes
- LeetCode 204:Count Primes
- React组件规范
- 6-17 Shortest Path [2](25 point(s))(dijkstra)
- KNN算法-小明逆袭的故事(1)
- java仿QQ截图带窗体检测
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- leetcode No204. Count Primes
- iOS 服务器文件有修改才进行下载
- Mysql常见报错处理
- SQL精妙语句
- java2017面试题新手看绝佳!!
- 范数与距离的关系以及在机器学习中的应用
- 配置spring所需要的jar包
- DVRRDK之MCFW下syslink操作
- spring相关注解