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. oracle入门(1)——安装oracle 11g x64 for windows

    [本文简介] 最近因为一个项目的需要,从零学习起了oracle,现在把学到的东西记录分享一下. 首先是安装篇,在win8 装10G 一直失败,网上各种方法都试过了,最后不得不放弃,选择了11G. 11 ...

  2. HTML5 canvas绘图基本使用方法

    <canvas></canvas>是HTML5中新增的标签,用于绘制图形,实际上,这个标签和其他的标签一样,其特殊之处在于该标签可以获取一个CanvasRenderingCon ...

  3. java 多线程 day06 threadLocal

    import java.util.HashMap;import java.util.Map;import java.util.Random; /** * Created by chengtao on ...

  4. Java读写.properties文件实例,解决中文乱码问题

    package com.lxk.propertyFileTest; import java.io.*; import java.util.Properties; /** * 读写properties文 ...

  5. JAVA 读取txt文件内容

    原文地址https://www.cnblogs.com/xing901022/p/3933417.html 通常,我们可以直接通过文件流来读取txt文件的内容,但有时可能会出现乱码!此时只要设置一下文 ...

  6. LeetCode:每日温度【739】

    LeetCode:每日温度[739] 题目描述 根据每日 气温 列表,请重新生成一个列表,对应位置的输入是你需要再等待多久温度才会升高的天数.如果之后都不会升高,请输入 0 来代替. 例如,给定一个列 ...

  7. 【工具】Notepad++ 上,代码格式化工具

    一.概述 Windows 自带的记事本功能太过简单,因此我常常使用 Notepad++ 查看文本.Notepad++ 支持插件功能,最近需要使用 Notepad++ 查看 Html 代码,而这些代码多 ...

  8. POJ 2516 Minimum Cost (KM最优匹配)

    题意:有N家家店,每家店都对K种货物有需求:同时有M家仓库,对K钟货物有供应.对于每种货物,每个仓库送至每家店都有自己的单位费用.求满足所有店所有货物的最小费用 分析:对于每一种货物,如果总需求大于总 ...

  9. netty3---传统IO,NIO,nettyIO

    传统: NIO: nettyIO: 每个服务生负责一个区域,每个服务生是一个线程.

  10. hadoop08---读写锁

    ReentrantLock 直接使用lock接口的话,我们需要实现很多方法,不太方便,ReentrantLock是唯一实现了Lock接口的类,并且ReentrantLock提供了更多的方法,Reen ...