LeetCode OJ:Count Primes(质数计数)
Count the number of prime numbers less than a non-negative number, n.
计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如下,写的有点乱不好意思。
class Solution {
public:
int countPrimes(int n) {
vector<int> vtor(n + , );
vector<int> ret;
for (int i = ; i <= n; ++i)
vtor[i] = i;
for (int i = ; i < n; ++i){//边界条件注意
if (vtor[i] != -){
ret.push_back(vtor[i]);
int tmpPrime = vtor[i];
for (int mul = ; tmpPrime * mul <= n; mul++){
vtor[tmpPrime * mul] = -;
}
}
}
return ret.size();
}
};
java版本的代码如下所示:
public class Solution {
public int countPrimes(int n) {
int [] prime = new int[n+]; //这里其实用boolean更好,懒得改了
int count = ;
for(int i = ; i < n+; ++i){
prime[i] = i;
}
for(int i = ; i < n+; ++i){
if(prime[i] == -)
continue;
else{
count++;
for(int mul = ; mul * i < n+; ++mul){
prime[mul*i] = -;
}
}
}
return count;
}
}
LeetCode OJ:Count Primes(质数计数)的更多相关文章
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode]Count and Say 计数和发言
Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- [LeetCode] Count Primes 质数的个数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- 【leetcode】Count Primes(easy)
Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...
- leetcode:Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...
随机推荐
- windows下的Mysql安装与基本使用(zip)
一.安装过程 Mysql社区版下载地址:http://dev.mysql.com/downloads/mysql/ --注意,已管理员身份运行cmd.exe,很重要!!目录在,c:\windows\s ...
- CRM项目问答总结
1. 通过ChangeList封装好多数据 DA: 在stark组件中,有五个封装的大类: class FilterOption(object): ----用于封装组合搜索的配置信息(数据库字段,是否 ...
- while小用
1.使用while打印1 2 3 4 5 6 8 9 10 #!/usr/bin/env python #encoding: utf-8 num = 1 while num < 11: if ...
- SharePoint 2010 以Jquery Ajax方式更新SharePoint列表数据!
之前本人的博客介绍了<sharepoint 2010自定义访问日志列表设置移动终端否和客户端访问系统等计算列的公式>,那如何通过Jquery提交访问日志到自定义的SharePoint的访问 ...
- 一行代码实现笔记本跳过微信认证连接WIFI
一行代码实现笔记本跳过微信认证连接WIFI 本文作者原创,没有参考其他文章,方法很简单但是很实用,转载请注明出处,谢谢! 问题 有一些WIFI需要通过微信认证才能连接,手机当然是可以的,但是我们手头的 ...
- Java Web架构总结
转载至:http://www.cnblogs.com/wuxl360/p/7489763.html 初始搭建 开始的开始,就是各种框架一搭,然后扔到Tomcat容器中跑就是了,这时候我们的文件,数据库 ...
- undefined symbol: PyUnicodeUCS4_AsUTF8String
python 默认是ucs2编码进行编译,重新编译使用ucs4. python: ./configure --enable-unicode=ucs4 make && ...
- OpenGL核心技术之Gamma校正
笔者介绍:姜雪伟,IT公司技术合伙人,IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你/2.2次幂.Gamma校正后的暗红色就会成为(0.5,0.0 ...
- NOIP 马拦过河卒
描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. 棋盘 ...
- javascript设计模式 - 解释器模式(interpreter)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...