LeetCode 204 Count Primes
Problem:
Count the number of prime numbers less than a non-negative number, n.
Summary:
判断小于某非负数n的质数个数。
Solution:
用所谓的“刷质数表”的方式先用HashTable记录小于n的所有数是质数还是合数,再逐一数出。
看了题目中的Hint才知道这种方法有一个更加高大上的名字Sieve of Eratosthenes
即在每判断一个数为质数时,将它的bei'shu倍数均计为合数。
class Solution {
public:
int countPrimes(int n) {
if (n == || n == ) {
return ;
}
vector<int> prime(n, );
prime[] = prime[] = ;
for (long long i = ; i < n; i++) {
if (!prime[i]) {
for (long long j = i * i; j < n; j += i) {
prime[j] = ;
}
}
}
int cnt = ;
for (int i = ; i < n; i++) {
if (!prime[i]) {
cnt++;
}
}
return cnt;
}
};
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 题 ...
- 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 的素数. 题目很简洁,但是算法实现的 ...
- LeetCode - 204. Count Primes - 埃拉托斯特尼筛法 95.12% - (C++) - Sieve of Eratosthenes
原题 原题链接 Description: Count the number of prime numbers less than a non-negative number, n. 计算小于非负数n的 ...
随机推荐
- 解决vs崩溃 无法打开工程 新建工程显示未找到约束
一般是因为windows更新引起的,可以删除与.net framework有关的更新补丁如果补丁太多可以试试如下方法: 解决方法: 1.关闭VS: 2.去C:/Users/<your users ...
- 【原】webp图片牛刀小试
其实今年很早就有接触到webp图片的概念,只是一直没怎么弄.今天在一个小项目中小用了一番.总结总结 采用 what,why,how的方式来总结 what? 什么是webp图片? 维基百科: ...
- python api
import requests #查询手机好归属地API def phone(tel): url = 'http://op.juhe.cn/onebox/phone/query' appkey = ' ...
- Linux常用命令和Shell编程基础
目录相关 cd - .与.. 分别表示当前目录和父目录 - ~与$HOME 都是指当前用户的主目录 - cd – 切换到上一次所在的目录(不一定是父目录) pwd - pwd 显示当前目录 - $PW ...
- PHP陷阱,一些注意事项
判断的一些注意事项 count(false) > 0 // true count(0) > 0 // true "随便一个字符串" == 0 // true " ...
- vtkTransform实例 1
1. 4*4矩阵类vtkMatrix4x4 接口函数:void SetElement(int i, int j, double value),i行.j列的值为value #ifndef INITIAL ...
- 【学习笔记】JAva编程思想之多态
1.如果java的基类拥有某个已被多次重载的方法名称,那么在导出类中重新定义该方法名称并不会屏蔽在基类的任何版本.因此,无论是在该层或者他的基类中对方法进行定义,重载机制都可以正常工作. 2.使用@O ...
- DOM0,DOM2,DOM3 事件基础知识
事件是javascript和HTML交互基础, 任何文档或者浏览器窗口发生的交互, 都要通过绑定事件进行交互; 事件有DOM0, DOM2和DOM3的区分(别问我怎么少了一个DOM1, 也没找到DOM ...
- WCF技术内幕 第二章 - 简单的Message
1.契约 - 接口 (客户端和服务端都要认识Message) namespace WCFService { [ServiceContract(Namespace = "http://wint ...
- dos命令行 指令
盘符: 例如想进入D盘 d: ../ 上一层目录 md 文件夹名 新建文件夹cd 文件夹名 进入到该目录cd.. 返回上一层目录cd\ 返回根目录 cd \windows 进入到当前盘Windows目 ...