Count the number of prime numbers less than a non-negative number, n.

计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如下,写的有点乱不好意思。

 class Solution {
public:
int countPrimes(int n) {
vector<int> vtor(n + , );
vector<int> ret;
for (int i = ; i <= n; ++i)
vtor[i] = i;
for (int i = ; i < n; ++i){//边界条件注意
if (vtor[i] != -){
ret.push_back(vtor[i]);
int tmpPrime = vtor[i];
for (int mul = ; tmpPrime * mul <= n; mul++){
vtor[tmpPrime * mul] = -;
}
}
}
return ret.size();
}
};

java版本的代码如下所示:

public class Solution {
public int countPrimes(int n) {
int [] prime = new int[n+]; //这里其实用boolean更好,懒得改了
int count = ;
for(int i = ; i < n+; ++i){
prime[i] = i;
}
for(int i = ; i < n+; ++i){
if(prime[i] == -)
continue;
else{
count++;
for(int mul = ; mul * i < n+; ++mul){
prime[mul*i] = -;
}
}
}
return count;
}
}

LeetCode OJ:Count Primes(质数计数)的更多相关文章

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

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

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

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

  3. [LeetCode]Count and Say 计数和发言

    Count and Say 计数和发言 思路:首先要理解题意,可以发现后者是在前者的基础之上进行的操作,所以我们拿之前的结果作为现在函数的参数循环n-1次即可,接下来就是统计字符串中相应字符的个数,需 ...

  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计数质数 (C++)

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

  6. [LeetCode] Count Primes 质数的个数

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

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

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

  8. LeetCode 204 Count Primes

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

  9. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  10. leetcode:Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. 本题给定一个非负数n,让我们求小于n ...

随机推荐

  1. 通信—HTTP与HTTPS

    HTTP是客户端浏览器或其他程序与Web服务器之间的应用层通信协议.在Internet上的Web服务器上存放的都是超文本信息,客户机需要通过HTTP协议传输所要访问的超文本信息. HTTPS(全称:H ...

  2. Webbench进行网站压力测试

    今天突然发现一个新大陆,Webbench,是linux下,用这很方便,开源,不限制并发访问次数和时间....大爱啊! 下载Webbench 使用wget  或者windows下载好导入linux也行, ...

  3. HadoopHA简述

    1 概述 在hadoop2.0之前,namenode只有一个,存在单点问题(虽然hadoop1.0有 secondarynamenode,checkpointnode,buckcupnode这些,但是 ...

  4. Spark学习笔记--安装SCALA和IDEA开发环境

    一:安装Scala

  5. java.util.Calendar简介

    Calendar是一个抽象类,我们无法直接实例化它,它有一个具体子类实体类java.util.GregorianCalendar,这个类实现的就是我们日常所用的公历历法,或者叫做阳历.我们可以直接使用 ...

  6. ASC转换BCD,ASC2BCD(转)

    int ASC2BCD(const char* szASC,byte* szBDC)   {   int szASCLen=strlen(szASC);   byte * bpBCD = new by ...

  7. Node单线程与异步编程的初步理解

    最近学习了一些关于node的单线程与异步的知识,想拿过来和大家分享下: var async = require('async') //并行无关联,等待事件为最长时间请求过程.如以下两个任务执行时间 c ...

  8. http://blog.csdn.net/dancing_night/article/details/46698853

    http://blog.csdn.net/dancing_night/article/details/46698853

  9. tesseract 3.05 release 编译

    tesseract 3.05 release版本的对应配置好的vs2015工程.偷懒必备,毕竟依赖那么多库,环境配置还是要费点事的.https://github.com/peirick/VS2015_ ...

  10. 利用Metasploit进行Linux提权

    利用Metasploit进行Linux提权 Metasploit 拥有msfpayload 和msfencode 这两个工具,这两个工具不但可以生成exe 型后门,一可以生成网页脚本类型的webshe ...