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的 ...
随机推荐
- GOF业务场景的设计模式-----观察者模式
定义:定义对象间一种一对多的依赖关系,使得当每一个对象改变状态,则所有依赖于它的对象都会得到通知并自动更新. 在软件系统中经常会有这样的需求:如果一个对象的状态发生改变,某些与它相关的对象也要随之做出 ...
- 找不到类型“{x}.{x}”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。
最近在搞一个WCF的项目... 刚开始在这条路上走... 各种崎岖... 网上搜到的一种解决方案(也是大多数情况的解决方案): 原文:http://www.cnblogs.com/Olive116/p ...
- JAVA之Socket编程
网上对Socket的诠释很多,也很全,在这里我就不多说了,总之,现在的网络处处都在使用Socket.本帖是一个Socket的例子,用来模拟一个简单的登录系统,只有核心代码,访问数据库.输入神马的统统没 ...
- POJ 2299 Ultra-QuickSort
离散化+树状数组求逆序数 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 35024 Accept ...
- ytkah网站建设解决方案 大中小微企业营销利器
为大中小微企业提供网站设计制作优化服务,PC移动微网站三合一,抢占市场先机.读万卷书不如走万里路,走万里路不如阅人无数.说再多空洞无物不如上案例几簇 优秀案例展示,上市公司人人网旗下游戏<天书奇 ...
- 动态导入(import)和静态导入(import)的区别
import static静态导入是JDK1.5中的新特性.一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com.....Cl ...
- Android Studio模拟器的问题及解决办法
1.could not bind gl_array_buffer error=0x502 环境:Windows10 解决办法: 在AVD Manager中,将模拟器的RAM设置为1G.
- 解决ScrollView里如果有动态更新的ChildView时会自动滚动到底部的方法
在这个ChildView的xml属性里加上 android:focusable="true" android:focusableInTouchMode="true&quo ...
- 跟着百度学PHP[4]OOP面对对象编程-5-内部引用$this
$this就是对象内部代表这个对象的引用 可以调用被封装的方法或者属性! <?php class Person{ private $name; "; var $sex; functio ...
- c++ 字符串流 sstream(常用于格式转换) 【转载】
使用stringstream对象简化类型转换C++标准库中的<sstream>提供了比ANSI C的<stdio.h>更高级的一些功能,即单纯性.类型安全和可扩展性.在本文中, ...