【LeetCode-面试算法经典-Java实现】【204-Count Primes(统计质数)】

【204-Count Primes(统计质数)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】


代码下载【https://github.com/Wang-Jun-Chao】

原题

  Description:

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

题目大意

  统计小于非负整数n的素数的个数。

解题思路

  使用见埃拉托色尼筛法。

代码实现

算法实现类

public class Solution {

    public int countPrimes(int n) {

        if (n <= 1) {
            return 0;
        }

        // 默认所有的元素值都会设置为false
        boolean[] notPrime = new boolean[n];
        notPrime[0] = true;
        notPrime[1] = true;

        for (int i = 2; i * i < n; i++) {
            // 如果i是一个质数,i将i的倍数设置为非质数
            // 如是i是一个合数,则它必定已经设置为true了,因为是从2开始处理的
            if (!notPrime[i]) {
                for (int j = 2 * i; j < n; j += i) {
                    notPrime[j] = true;
                }
            }
        }

        // 统计质数的个数
        int result = 0;
        for (boolean b : notPrime) {
            if (!b) {
                result++;
            }
        }

        return result;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/48021413

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-02 20:17:19

【LeetCode-面试算法经典-Java实现】【204-Count Primes(统计质数)】的相关文章

LeetCode:Count Primes - 统计质数数量

1.题目名称 Count Primes(统计质数数量) 2.题目地址 https://leetcode.com/problems/count-primes/ 3.题目内容 英文:Count the number of prime numbers less than a non-negative number, n. 中文:统计正整数n以内(不含n本身)质数的数量 4.一个TLE的方法 从1到n,考察每个数字是否为质数.这个方法由于花费时间较长,不能满足题目中对时间的要求. 一段实现此方法的Jav

LeetCode 204 Count Primes(质数计数)(*)

翻译 计算小于一个非负整数n的质数的个数. 原文 Count the number of prime numbers less than a non-negative number, n. 分析 这道题以前遇到过,当时是用的最笨的办法,现在也没什么好想法,又恰好题目有提示,我就点开了.题目的提示是一条一条给出来的,我也就逐个的全点开了,感觉好失败-- public int countPrimes(int n) { int count = 0; for (int i = 1; i < n; i++

【LeetCode-面试算法经典-Java实现】【054-Spiral Matrix(螺旋矩阵)】

[054-Spiral Matrix(螺旋矩阵)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]

【LeetCode-面试算法经典-Java实现】【025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)】

[025-Reverse Nodes in k-Group(单链表中k个结点一组进行反转)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If the number of nodes is not a multiple of k then left-out nodes i

【LeetCode-面试算法经典-Java实现】【139-Word Break(单词拆分)】

[139-Word Break(单词拆分)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, given s = "leetcode", di

【LeetCode-面试算法经典-Java实现】【137-Single Number II(只字出一次的数字II)】

[137-Single Number II(只出现一次的数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you imple

【LeetCode-面试算法经典-Java实现】【061-Rotate List(旋转单链表)】

[061-Rotate List(旋转单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 题目大

【LeetCode-面试算法经典-Java实现】【075-Sort Colors (颜色排序)】

[075-Sort Colors (颜色排序)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the

【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】

[038-Count and Say(计数和表述)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, ... 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21