Leetcode(59)-Count Primes

题目:

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;
    }
}
时间: 2024-07-31 23:25:01

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

LeetCode(38) Count and Say

题目 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. 21 is read off as "one 2, then one 1" or 1211. Given

LeetCode 第 204 题 (Count Primes)

LeetCode 第 204 题 (Count Primes) Description: Count the number of prime numbers less than a non-negative number, n. 计算小于 N 的素数的个数.这道题目比较简单.但是想提高计算效率与需要费点脑筋. 判断一个数字 n 是不是素数的简单方法是 用 n 去除 2,3,4,-,n?1,如果都不能整除就说明这个数是素数. 按照这个思路可以写个简单的函数. bool isPrime(int n)

Leetcode(5)最长回文子串

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 一开始我的思路如下:回文子串的特点是首尾字母相同,所以我对每一个字母都找到位于它后面的相同字母,利用切片判断这一段是否为回文子串(str[i:j]==str[i:j][::-1]).时间复杂度很高,主要是因为str.find操作非常耗时. class Solution(object): def lo

每天努力一点之SQL(二) count sum case when then group by

1. select sum(CASE WHEN A.[STATUS]=0 THEN 1 ELSE 0 end) as a1,  sum(CASE A.[STATUS] WHEN 1 THEN 1 ELSE 0 end) as a2,  sum(CASE A.[STATUS] WHEN 2 THEN 1 ELSE 0 end) as a3, A.UserId,C.TrainId  from CoursewareLogMiddle A join LessonDetail B   on A.Lesso

Leetcode(4)寻找两个有序数组的中位数

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1* 和 nums2 不会同时为空. 第一种方法:list拼接排列取中位数 执行用时:116 ms : 内存消耗:11.8MB 效果:还行 class Solution(object): def findMedianSortedArrays(self,

Leetcode(1)两数之和

Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 第一种方法:暴力 执行用时:5352 ms: 内存消耗:12.9MB 效果:非常差 class Solution(object): def twoSum(self, nums, target): """ :type nums:

Leetcode(2)两数相加

Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 第一种方法:大众解法 执行用时:80 ms: 内存消耗:12.2MB # Definition for singly-linked list. # class ListNode(object

Leetcode(3)无重复字符的最长子串

Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果:太差 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ Maxsize=0 res='' if len(s)

Leetcode(9)回文数

Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4MB 效果:还行 class Solution: def isPalindrome(self, x: int) -> bool: s=str(x) if s==s[::-1]: return True else: return False 第二种方法:反转一半数字 执行用时:156 ms: 内存消耗