204. Count Primes - LeetCode
Queston
Solution
题目大意:给一个数,求小于这个数的素数的个数
思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为false,再遍历3并将3的倍数置为false...
Java实现:
此法超时
public int countPrimes(int n) {
// 2, 3, 5, 7
boolean[] candidate = new boolean[n];
Arrays.fill(candidate, Boolean.TRUE);
candidate[0] = false;
candidate[1] = false;
int count = 0;
for (int i=2; i<n; i++) {
if (candidate[i]) {
count++;
for (int j = i+1; j<n; j++) {
if (j%i==0) candidate[j] = false;
}
}
}
return count;
}
参考别人的:
public int countPrimes(int n) {
boolean[] notPrime = new boolean[n];
int count = 0;
for (int i = 2; i < n; i++) {
if (notPrime[i] == false) {
count++;
for (int j = 2; i*j < n; j++) {
notPrime[i*j] = true;
}
}
}
return count;
}
204. Count Primes - LeetCode的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- [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 ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
随机推荐
- java中如何知道一个字符串中有多少个字,把每个字打印出来,举例
9.6 About string,"I am ateacher",这个字符串中有多少个字,且分别把每个字打印出来. public class Test { static i ...
- 微信小程序HTTP接口请求封装
1.方法封装(新建文件夹util,工具文件,在文件夹下创建request.js文件,用于对方法封装)request.js: var app = getApp(); //项目URL相同部分,减轻代码量, ...
- ASP.NET Core高性能服务器HTTP.SYS
如果我们只需要将ASP.NET CORE应用部署到Windows环境下,并且希望获得更好的性能,那么我们选择的服务器类型应该是HTTP.SYS.Windows环境下任何针对HTTP的网络监听器/服务器 ...
- 序列化和反序列化为什么要实现Serializable接口?(史上最全、简单易懂)
目录结 前言 1.什么是序列化和反序列化 2.什么时候需要进行序列化和反序列化 2.1.服务器和浏览器交互时用到了Serializable接口吗? 2.2.Mybatis将数据持久化到数据库中用到了S ...
- Spring Boot-使用Spring Initializer快速创建Spring Boot项目
File->project->Spring Initializer 点击next 点击下一步即可,如果是第一次可能需要下载jar包,如下图 resources文件中的目录结构如上图所示 s ...
- 安全市场迎来新挑战,FinClip助力车联网数据安全
随着汽车工业的发展与电子技术的进步,智能汽车迎来了前所未有的蓬勃发展,随着汽车电动化.网联化.智能化交融发展,车辆运行安全.数据安全和网络安全风险交织叠加,安全形势更加复杂严峻......
- 小程序已成为超级APP必选项,逐鹿私域“留量”
截止2021年底,中国移动互联网月活跃用规模达到11.74亿人,增速逐渐呈放缓趋势,用户渗透率接近天花板.客户的增长速度越趋于平缓,品牌在不同成长阶段也要适应增长节奏的变化,越来越多主流商家不得不利用 ...
- python中faker模块的使用
Faker 安装 pip install Faker 基本使用 from faker import Faker #创建对象,默认生成的数据为为英文,使用zh_CN指定为中文 fake = Faker( ...
- Golang 源码解读 01、深入解析 strings.Builder、strings.Join
strings.Builder 源码解析. 存在意义. 实现原理. 常用方法. 写入方法. 扩容方法. String() 方法. 禁止复制. 线程不安全. io.Writer 接口. 代码. stri ...
- Codeforces Round #705 (Div. 2) B. Planet Lapituletti(镜像时钟) 思维
传送门: https://codeforces.com/contest/1493/problem/B 题目: Example input 5 24 60 12:21 24 60 23:59 90 8 ...