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

思路:数质数的个数

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

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

int countPrimes(int n) {
if(n <= ) return ;
bool * mark = new bool[n];
fill_n(mark, n, true); int primesNum = ;
int curpos = ;
while(curpos < n)
{
//把当前质数的倍数标记为不是质数
for(int i = * curpos; i < n; i += curpos)
{
mark[i] = false;
} //找下一个质数
int i;
for(i = curpos + ; 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-,sizeof(bool)); int ret_c=;
// idx 0 represent 2
int idx=;
int pend=n-;
while(idx<pend){
if(==pb[idx]){
++ret_c;
int op=idx;
while(op<pend){
pb[op]=;
op+=(idx+);
}
}
++idx;
}
free(pb);
return ret_c;
}

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

  1. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  2. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 【leetcode】Min Stack(easy)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  5. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

  6. 【leetcode】Word Break (middle)

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  7. 【LeetCode】Reconstruct Itinerary(332)

    1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...

  8. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  9. 【LeetCode】Counting Bits(338)

    1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...

随机推荐

  1. 2015年11月26日 Java基础系列(四)class的定义,继承和实现interface

    序,类的设计是JAVA操作的核心,面对对象思想中一切皆对象. 一.类定义中的变量 静态成员变量,为类所有,称为类变量:只有一份,编译时即分配值,使用关键字static声明. 非静态成员变量,每个实例一 ...

  2. "当前方法的代码已经过优化,无法计算表达式的值"的这个错误的解决方案!!!

    http://blog.useasp.net/archive/2012/09/12/how-to-debug-dotnet-framework-source-when-throw-the-code-o ...

  3. 欧几里得证明$\sqrt{2}$是无理数

    选自<费马大定理:一个困惑了世间智者358年的谜>,有少许改动. 原译者:薛密 \(\sqrt{2}\)是无理数,即不能写成一个分数.欧几里得以反证法证明此结论.第一步是假定相反的事实是真 ...

  4. gspx请求周期(备忘)

  5. Apache 虚拟主机

    httpd支持的虚拟主机类型包括以下三种 基于域名:为每个虚拟主机使用不同的域名.但其对应的IP使相同的. 基于IP地址:为每个虚拟主机使用不同的域名,切各自对应的IP地址也不相同. 基于端口:这种方 ...

  6. 关于tableView的错误提示

    WARNING: Using legacy cell layout due to delegate implementation of tableView:accessoryTypeForRowWit ...

  7. Sqli-LABS通关笔录-16

    这个关卡之前我还使用了一下工具跑,发现居然跑不出来.这就尴尬了.行吧手工试试. payload admin") and If(ascii(substr(database(),1,1))=11 ...

  8. Button圆角处理

    <?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="ht ...

  9. caffe学习系列(5):激活层介绍

    参考:http://www.cnblogs.com/denny402/p/5072507.html 主要介绍了各个激活函数.

  10. mysql互换表中两列数据方法

    1.创建表及记录用于测试 ) unsigned ) ,) unsigned ,) unsigned NOT NULL COMMENT '现价', PRIMARY KEY (`id`) ) ENGINE ...