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的更多相关文章

  1. [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数

    题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...

  2. Java [Leetcode 204]Count Primes

    题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...

  3. [LeetCode] 204. Count Primes 质数的个数

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  4. [LeetCode] 204. Count Primes 计数质数

    Description: Count the number of prime numbers less than a non-negative number, n click to show more ...

  5. LeetCode 204. Count Primes (质数的个数)

    Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...

  6. LeetCode 204 Count Primes

    Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...

  7. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  8. [LeetCode] 204. Count Primes 解题思路

    Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...

  9. 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的 ...

随机推荐

  1. Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)

    1  Python学习网址:http://www.runoob.com/python/python-multithreading.html

  2. 利用flexbox实现按字符长度排列dom元素

    说明:请使用chrome浏览器打开 See the Pen pvyjGV by lilyH (@lilyH) on CodePen. 如上图所示,我们你要实现的效果就是,(1)在一行中显示两块元素:( ...

  3. ACM_1 大数求和

    /*1 *2014.11.18 *大数求和 */ #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <strin ...

  4. PL/SQL创建数据表空间

    创建数据表空间create tablespace stbss datafile 'E:\oracle\product\10.2.0\oradata\orcl\stbss_temp01.dbf' siz ...

  5. CSS相邻兄弟选择器

    相邻兄弟选择器定义:选择紧接在另一个元素后的元素,而且两者有着相同的父元素. 代码一:<body> <h1>This is a heading.</h1> < ...

  6. Bellman_Ford

    Source:http://www.cnblogs.com/Jason-Damon/archive/2012/04/21/2460850.html 摘自百度百科 Bellman-ford算法是求含负权 ...

  7. iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html

    iOS/Android/Web Url Encode空格處理 原文連結:http://read01.com/3gDO.html 前言 這裡只是講一個故事,一個發生在我身上的真實的故事.曾經,我以為搞加 ...

  8. php中mysql与mysqli的区别

    两个函数都是用来处理DB 的.首先, mysqli 连接是永久连接,而mysql是非永久连接. mysql连接每当第二次使用的时候,都会重新打开一个新的进程,而mysqli则只使用同一个进程,这样可以 ...

  9. 跟着百度学PHP[4]OOP面对对象编程-13-魔术方法__set(),__get(),__isset(),__unset()

    __set() 在对象访问私有成员的时候自动被调用,达到了给你看,但是不能给你修改的效果!(在对象访问一个私有的成员的时候就会自动的调用该魔术方法) __get() 方法用于获取私有属性值.(在设置私 ...

  10. 51Nod 1250 排列与交换

    Description 统计 \(1...n\) 的排列,恰好进行 \(k\) 次相邻交换和至多进行 \(k\) 次交换生成的不同的序列个数. Sol DP. 好妙的题啊... 首先看第一个问题. 对 ...