博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
204. Count Primes
阅读量:7041 次
发布时间:2019-06-28

本文共 752 字,大约阅读时间需要 2 分钟。

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

Example:

Input: 10Output: 4Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.

难度:easy

题目:统计小于非负整数n的所有素数。

思路:参考素数筛选法。

Runtime: 11 ms, faster than 94.69% of Java online submissions for Count Primes.

Memory Usage: 35.9 MB, less than 21.43% of Java online submissions for Count Primes.

class Solution {    public int countPrimes(int n) {        boolean[] notPrimes = new boolean[n];        int count = 0;        for (int i = 2; i < n; i++) {            if (!notPrimes[i]) {                count++;                for (int j = 2 * i; j < n; j += i) {                    notPrimes[j] = true;                }            }        }                return count;    }}

转载地址:http://klxal.baihongyu.com/

你可能感兴趣的文章
通过polyfill了解JS(一)
查看>>
深度有趣 | 29 方言种类分类
查看>>
态度这么差,我不敢要你啊?
查看>>
mybatis+dubbo+ springmvc+zookeeper分布式架构
查看>>
vue实现三级省市区三级联动
查看>>
学习JavaScript正则表达式之——字符匹配攻略 #1
查看>>
InnoDB存储引擎MVCC实现原理
查看>>
西瓜书学习-神经网络
查看>>
[译] 如何用 CSS Animations 实现滑动图片展现文字的效果
查看>>
Zend Studio使用教程:使用Zend Studio和Zend Server进行根本原因分析 (二)
查看>>
golang的fmt包String(),Error(),Format(),GoString()的接口实现
查看>>
Java技术转(兼顾)产品经理——读《快速转行做产品经理》有感
查看>>
成为优秀Java开发人员的10件事
查看>>
Kali Linux安装教程
查看>>
mysql客户端pymysql在python下性能比较
查看>>
Android缓存处理
查看>>
JavaScript 数据类型检测终极解决方案
查看>>
年赚百万游戏主播!玩转Python后:几行代码轻松“吃鸡” 附源码
查看>>
【python】使用简单的python语句编写爬虫 定时拿取信息并存入txt
查看>>
卡拉OK歌词原理和实现高仿Android网易云音乐
查看>>