LeetCode Count Primes
2015-08-03 02:57
405 查看
原题链接在这里:https://leetcode.com/problems/count-primes/
题目:
Description:
Count the number of prime numbers less than a non-negative number, n.
题解:
网上查查,原来有一种方法叫:Sieve of Eratosthenes 的方法。时间复杂度为O(nloglogn),空间复杂度为O(n).
AC Java:
跟上Perfect Squares
题目:
Description:
Count the number of prime numbers less than a non-negative number, n.
题解:
网上查查,原来有一种方法叫:Sieve of Eratosthenes 的方法。时间复杂度为O(nloglogn),空间复杂度为O(n).
AC Java:
public class Solution { public int countPrimes(int n) { if(n < 3){ return 0; } boolean [] isPrime = new boolean ; Arrays.fill(isPrime, true); int res = n-2; for(int i = 2; i*i<n; i++){ if(isPrime[i]){ for(int j = i; i*j<n; j++){ if(isPrime[i*j]){ isPrime[i*j] = false; res--; } } } } return res; } }
跟上Perfect Squares
相关文章推荐
- IO
- JavaMail
- PHP之GD库
- 将dll放进exe[.Net]
- Java Annotation
- c#的DateTime.Now函数详解
- Java_Collection
- XSS 平台搭建与优化(基于 xsser.me 源码)
- 用Mediawiki做百科网站资源大参考
- 用Mediawiki做百科网站资源大参考
- Leetcode #88 Merge Sorted Array
- ubutun 下webalizer 分析Apache日志
- 网站流量统计系统 phpMyVisites
- ubutun 下webalizer 分析Apache日志
- 网站流量统计系统 phpMyVisites
- Ntop监控网络流量
- Ntop监控网络流量
- Java实现点链表
- Qt与QtWebKit
- mrtg监控网络流量简单配置