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 ...
随机推荐
- oracle入门(4)——少而常用的命令
[本文介绍] 本文将介绍使用oracle的常用命令,不是”大全“,但少而实用. 下面的命令都是在项目用到的才记录下来的,如果以后用到其他的,会不断更新. [命令介绍] 描述: 命令: [数据库] ...
- 编程语言的介绍(Day2)
1.什么是编程,为什么要编程? 编程==编写程序(写代码) 2.编程语言有哪些 机器语言 优点是最底层,速度最快,缺点是最复杂,开发效率最低 汇编语言 优点是比较底层,速度最快,缺点是复杂,开发效率最 ...
- C#:DateTime格式增减时间
DateTime time = DateTime.Now(); DateTime XMinutesrLater = time.AddMinutes(X); // X分钟以后的时间,即在当前时间基础上增 ...
- iOS XCode工程 警告处理
今天 老板说,群~你的警告⚠️蛮多的...我拍了胸脯,下周项目总结时候一定会完美解决!!! 于是我得把项目中全部警告解决了,加油
- python入门四:异常
一.异常 异常就是在触发异常条件时(解释器或程序员)而采取相应的措施 c++中异常使用try, throw, catch等关键字,而python中使用try, raise, except等 二.标准异 ...
- WPF MVVM TreeView 实现 右键选中 右键菜单
1.非MVVM模式:下载源代码WpfApplication1.zip <TreeView Height="200" PreviewMouseRightButtonDown=& ...
- Windows:FTP命令大全
Windows:FTP命令大全 简介 1, open:与服务器相连接: 2, send(put):上传文件: 3,get:下载文件: 4,mget:下载多个文件: 用法: mget *:下载当前路径下 ...
- Saltstack 命令行:批量发送命令,返回执行结果
批量发送发送命令符,并返回结果. salt '*' cmd.run 'df -h' ---------------------------------------- Stest1: Filesyste ...
- Java设计模式学习之工厂模式
在Java(或者叫做面向对象语言)的世界中,工厂模式被广泛应用于项目中,也许你并没有听说过,不过也许你已经在使用了.Java 设计模式之工厂模式 简单来说,工厂模式的出现源于增加程序序的可扩展性,降低 ...
- 会话控制Session的应用
Session技术与Cookie相似,都是用来存储使用者的相关资料.但是最大不同之处在于Cookie是将数据存放于客户端计算机中,而Session则是将数据存放于服务器系统下. 在Web技术发展史上, ...