题目:

Description:

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

思路:

  • 题意:求小于给定非负数n的质数个数
西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少了逐一检查每个数的的步骤,可以比较简单的从一大堆数字之中,筛选出质数来,这方法被称作厄拉多塞筛法(Sieve of Eeatosthese)。

具体操作:先将 2~n 的各个数放入表中,然后在2的上面画一个圆圈,然后划去2的其他倍数;第一个既未画圈又没有被划去的数是3,将它画圈,再划去3的其他倍数;现在既未画圈又没有被划去的第一个数 是5,将它画圈,并划去5的其他倍数……依次类推,一直到所有小于或等于 n 的各数都画了圈或划去为止。这时,表中画了圈的以及未划去的那些数正好就是小于 n 的素数。

代码:

public class Solution {
    public int countPrimes(int n) {
        if(n < 3){
            return 0;
        }
        boolean[] prime =new boolean[n];
        prime[2] = false;
        for(int i = 3;i < n;i++){
            if(i % 2 == 0){
                prime[i] = true;
            }else{
                prime[i] = false;
            }
        }
        for(int a = 3;a < n;a= a+2){
            if(!prime[a]){
                if(a * a < n){
                    for(int j = 2;a * j < n;j++){
                    prime[a*j] = true;
                    }
                }
            }
        }
        int count = 0;
        for(int b = 2;b < n;b++){
            if(!prime[b]){
                count++;
            }
        }
        return count;
    }
}

Leetcode(59)-Count Primes的更多相关文章

  1. Leetcode(204) Count Primes

    题目 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Speci ...

  2. LeetCode(38) Count and Say

    题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...

  3. LeetCode(59):螺旋矩阵 II

    Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...

  4. LeetCode(59)Length of Last Word

    题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...

  5. LeetCode(59)SPiral Matrix II

    题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  6. Leetcode(5)最长回文子串

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...

  7. Qt 学习之路 2(59):使用流处理 XML

    Qt 学习之路 2(59):使用流处理 XML 豆子 2013年7月25日 Qt 学习之路 2 18条评论 本章开始我们将了解到如何使用 Qt 处理 XML 格式的文档. XML(eXtensible ...

  8. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  9. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

随机推荐

  1. numpy教程:排序、搜索和计数

    http://blog.csdn.net/pipisorry/article/details/51822775 numpy排序.搜索和计数函数和方法.(重新整合过的) ],, , ], [, , ]] ...

  2. 剑指offer面试题4 替换空格(java)

    注:利用java中stringBuilder,append,length方法很方便的解决字符串问题 /* * 剑指offer 替换空格 * xsf * */ /*开始替换空格的函数,length为原数 ...

  3. testng的使用

    TestNG教程 TestNG是一个测试框架,其灵感来自JUnit和NUnit,但同时引入了一些新的功能,使其功能更强大,使用更方便. TestNG设计涵盖所有类型的测试:单元,功能,端到端,集成等, ...

  4. 【IOS 开发】Objective - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射

    一. Objective-C 对象简单处理 1. 包装类 (1) 包装类简介 NSValue 和 NSNumber : -- 通用包装类 NSValue : NSValue 包装单个 short, i ...

  5. Android View框架总结(一)

    View和Activity的区别 View有哪些? ViewGroup是什么? 为什么Google产生ViewGroup? View的层级结构是什么? View的onMeasure()/onLayou ...

  6. 【一天一道LeetCode】#219. Contains Duplicate II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  7. ROS_Kinetic_23 ROS流行版本和相关书籍汇总

    目前,ROS使用的主流版本主要是下面四种:Hydro,Indigo,Jade,Kinetic. Distro Release date Poster Tuturtle, turtle in tutor ...

  8. Ubuntu15.10下如何使用EasyGui模块开发Python GUI

    偶然的一个机会,发现了github上的这个开源的项目,easygui for python(一个基于TKinter的模块) 感觉很是惊讶,原来python也可以这么简单的开发出一些GUI界面(究其原因 ...

  9. 并发编程之ThreadLocal、Volatile、synchronized、Atomic关键字扫盲

    前言 对于ThreadLocal.Volatile.synchronized.Atomic这四个关键字,我想一提及到大家肯定都想到的是解决在多线程并发环境下资源的共享问题,但是要细说每一个的特点.区别 ...

  10. 5个你不知道的HTML5的接口

    原文地址:5 HTML5 APIs You Didn't Know Existed 原文日期: 2010年09月27日 翻译日期: 2013年8月7日 当人们看到或者说出"HTML5&quo ...