Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数。
作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法。
是的,素数筛法并不是该题最佳的解法,线性素数筛法才是。
至于什么是素数筛法,请百度吧。
class Solution {
public:
int countPrimes(int n) {
bool *isp= new bool[n];
for (int i = ; i < n; ++i)
{
isp[i]= true;
}
for (int i = ; i * i< n; ++i)//素数筛法
{
if(isp[i]){
for(int j = i + i; j < n; j+=i){
isp[j] = false;
}
}
}
int ans = ;
for (int i = ; i < n; ++i){
ans += isp[i];
}
delete isp;
return ans;
}
};
Leetcode 204 Count Primes 数论的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [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 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- 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的质数 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
随机推荐
- Selenium2+python自动化13-多窗口、句柄(handle)
前言 有些页面的链接打开后,会重新打开一个窗口,对于这种情况,想在新页面上操作,就得先切换窗口了.获取窗口的唯一标识用句柄表示,所以只需要切换句柄,我们就能在多个页面上灵活自如的操作了. 本篇以打开百 ...
- cmd.ExecuteReader(CommandBehavior.CloseConnection)
有些开发人员坚持认为,如果您设置 CommandBehavior.CloseConnection 选项,则 DataReader 及其相关联的连接会在 DataReader 完成数据读取时自动关闭.这 ...
- 6.HotSpot垃圾收集器
HotSpot JVM收集器 上面有7中收集器,分为两块,上面为新生代收集器,下面是老年代收集器.如果两个收集器之间存在连线,就说明它们可以搭配使用. 并发和并行 先解释下什么是垃圾收集器的上下文语境 ...
- Linux上安装php
1.安装mysql http://blog.csdn.net/wy3552128/article/details/8143686 2.安装配置Apache http://blog.csdn.net ...
- 回忆读windows 核心编程
看<windows 核心编程> 第五版到纤程了,下一章节即将介绍内存体系编程.如果做window平台下的开发,我感觉此书一定要读.记得开始讲解了window的基础,然后讲解内核对象.内核对 ...
- WPF中ListBox的样式设置
设置之后的效果为
- redis hash map
redis hash的使用详见文章:http://www.miaoyueyue.com/archives/235.html hash操作命令如下: hset(key, field, value):向名 ...
- 梳理一下JS的基本语法
web三层结构来说 结构层:HTML 从语义角度,描述页面结构 样式层:CSS 从审美的角度,美化页面 行为层:JavaScript 从交互的角度,提升用户体验 ...
- aceAdmin fuelux tree 从后台获取数据,并设置节点ID等属性
如题,从后台封装数据,有两种方式渲染节点的数据: 1.全部节点加载 2.根据父节点加载子节点 首先,先介绍下第一种渲染方式: 后台返回数据格式(所有的附加属性,都可放在additionalParame ...
- StringGrid 实例3: 本例功能: 1、修改 TStringGrid的默认宽与高; 2、添加行; 3、确认当前单元并赋值.
实例3: 本例功能: 1.修改 TStringGrid的默认宽与高; 2.添加行; 3.确认当前单元并赋值. 实例图形: