204. Count Primes
来源:互联网 发布:java方法调用范围 编辑:程序博客网 时间:2024/06/17 20:03
Count the number of prime numbers less than a non-negative number, n.
大致思路差不多 答案要简洁一些 注意结束条件 不是 i<num/2 而是i*i<=num
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;}
it starts from 2, the first prime, then mark the multip of 2 as true in notPrime, so the loop of i will skip them. the next prime is 3, do the same thing. Then it is 4, which 2*2, so the not prime is true, and will skip to next.
阅读全文
0 0
- 204. Count Primes
- 【LeetCode】204.Count Primes
- [leetcode] 204.Count Primes
- LeetCode 204. Count Primes
- Leetcode 204. Count Primes
- 204. Count Primes
- 204. Count Primes
- [LeetCode]204. Count Primes
- 204. Count Primes LeetCode
- 204. Count Primes
- 204. Count Primes
- 204. Count Primes
- 204. Count Primes
- LeetCode *** 204. Count Primes
- 204. Count Primes
- LeetCode 204. Count Primes
- leetcode-204. Count Primes
- 204. Count Primes
- 水仙花数。论求三位数中的各位数字。
- PSR-1规范
- 扩展欧几里德算法(待补充)
- 错误处理:"Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was
- Echarts表格数据转换工具
- 204. Count Primes
- 什么是Spring的依赖注入?
- Linux下常用命令及快捷键
- 如何将本地项目变成git项目并关联远程仓库
- Ubntu中命令使用
- 织梦dede {dede:field }标签里怎么用if {dede:if field.IsLock ==0}
- 编写守护进程(待优化)
- css中的px,em&rem
- 数据建模词根设计的若干原则(一)