Java for LeetCode 204 Count Primes
Description:
Count the number of prime numbers less than a non-negative number, n.
解题思路:
空间换时间,开一个空间为n的数组,因为非素数至少可以分解为一个素数,因此遇到素数的时候,将其有限倍置为非素数,这样动态遍历+构造下来,没有被设置的就是素数。
public int countPrimes(int n) {
if (n <= 2)
return 0;
boolean[] notPrime = new boolean[n];
int res = 0;
int bound = (int) Math.sqrt(n);
for (int i = 2; i < n; i++) {
if (!notPrime[i]) {
res++;
if (i <= bound)
for (int j = i * i; j < n; j += i)
notPrime[j] = true;
}
}
return res;
}
Java for LeetCode 204 Count Primes的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- 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 质数的个数
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. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数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的 ...
随机推荐
- va_list深究
va_list深究 2011-04-21 21:06:11| 分类: C/C++|字号 订阅 VA函数(variable argument function),参数个数可变函数,又称可变参数 ...
- Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)
1 Python学习网址:http://www.runoob.com/python/python-multithreading.html
- Sublime Text 3初阶
本文主要介绍一些Sublime Text3的初级阶段,主要从最初的安装,到插件,还有主题这三个方面介绍,还会提到一些关于使用ST3的一些小小经验... 一:安装 首先进入sublime的官方地址去下载 ...
- jQuery回调、递延对象总结(中篇) —— 神奇的then方法
前言: 什么叫做递延对象,生成一个递延对象只需调用jQuery.Deferred函数,deferred这个单词译为延期,推迟,即延迟的意思,那么在jQuery中 又是如何表达延迟的呢,从递延对象中的t ...
- 【JavaScript】JS_Object跟Function的区别
JS_Object和Function的区别 我们本次的解释,主要通过下图 粗看该图,估计你不一定能看明白.不过接下来让我逐行向你解释. 最左侧:意思是,有两个对象f1和f2,他们是通过new Foo( ...
- 优酷土豆2014校园招聘笔试题目之Java开发类
先总体说下题型,共有20道选择题,4道简答题,3道编程题和1道扩展题,题目都比较简单,限时一小时完成. 一.选择题 选择题非常简单,都是基础题,什么死锁发生的条件.HashMap和HashSet查找插 ...
- TTFB-首字节时间简介
百度站长工具里看到有一个"首字节时间"的建议,第一次听说,还真不知道是什么东东.百度站长工具里面的解释是:"浏览器开始收到服务器响应数据的时间=后台处理时间+重定向时间, ...
- 【C语言入门教程】5.2 函数的作用域规则(auto, static)
作用域规则是指代码或数据的有效使用范围.C语言将函数作为独立的代码块,函数之间不能相互访问其内部的代码或数据.函数间数据的传递只能通过接口实现.但是,变量的定义方法可改变函数的作用域规则,可将变量分为 ...
- C#获取当前时间与同步时间
http://blog.163.com/ljq086@126/blog/static/549639712010112921658843/ 我们可以通过使用DataTime这个类来获取当前的时间.通过 ...
- Codeforces Round #288 (Div. 2) E. Arthur and Brackets
题目链接:http://codeforces.com/contest/508/problem/E 输入一个n,表示有这么多对括号,然后有n行,每行输入一个区间,第i行的区间表示从前往后第i对括号的左括 ...