【leetcode】Count Primes(easy)

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

思路:数质数的个数

开始写了个蛮力的,存储已有质数,判断新数字是否可以整除已有质数。然后妥妥的超时了(⊙v⊙)。

看提示,发现有个Eratosthenes算法找质数的,说白了就是给所有的数字一个标记,把质数的整数倍标为false,那么下一个没被标为false的数字就是下一个质数。

int countPrimes(int n) {
        if(n <= 2) return 0;
        bool * mark = new bool[n];
        fill_n(mark, n, true);

        int primesNum = 1;
        int curpos = 2;
        while(curpos < n)
        {
            //把当前质数的倍数标记为不是质数
            for(int i = 2 * curpos; i < n; i += curpos)
            {
                mark[i] = false;
            }

            //找下一个质数
            int i;
            for(i = curpos + 1; i < n && !mark[i]; ++i);
            if(i == n)
            {
                delete [] mark;
                return primesNum; //没有多余的质数 返回答案
            }
            else
            {
                curpos = i;
                primesNum++;
            }
        }
    }

别人写的C版本的:

int countPrimes(int n) {
    bool *pb = calloc(n-1,sizeof(bool));

    int ret_c=0;
    // idx 0 represent 2
    int idx=0;
    int pend=n-2;
    while(idx<pend){
        if(0==pb[idx]){
            ++ret_c;
            int op=idx;
            while(op<pend){
                pb[op]=1;
                op+=(idx+2);
            }
        }
        ++idx;
    }
    free(pb);
    return ret_c;
}
时间: 2024-11-01 14:00:34

【leetcode】Count Primes(easy)的相关文章

【leetcode】Happy Number(easy)

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

【leetcode】Remove Element (easy)

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 思路: s记录下一个判断位置, e记录结束位置,把前面的待排除元素与后面要保留的元素互换. int removeE

【leetcode】Same Tree(easy)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:太简单! bool isSameTree(TreeNode *p, TreeNode *q) { if(p == NULL &

【leetcode】Reverse Integer(middle)☆

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出的方法 ①用数据类型转换long  或 long long ②在每次循环时先保存下数字变化之前的值,处理后单步恢复看是否相等 (比③好) ③整体恢复,看数字是否相等. 思路:注意30000这样以0结尾的数字,注意越界时返回0. 我检查越界是通过把翻转的数字再翻转回去,看是否相等. int rever

【leetcode】Count Primes

Description:Count the number of prime numbers less than a non-negative number, n. 1 class Solution { 2 public: 3 int countPrimes(int n) { 4 vector<bool> num(n-1,true); 5 num[0]=false; 6 int res=0; 7 int limit=sqrt(n); 8 9 for(int i=2;i<=limit;i++

【leetcode】Word Break (middle)

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, givens = "leetcode",dict = ["leet", "code"]. Return true because &

【leetcode】House Robber (middle)

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will autom

【leetcode】3Sum Closest(middle)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【leetcode】Partition List(middle)

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2