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月30日 spring初级知识讲解(一)装配Bean

    序,Spring的依赖注入是学习spring的基础.IOC为控制反转,意思是需要的时候就由spring生成一个,而不是先生成再使用. 写在前面 Spring提供面向接口编程,面向接口编程与依赖注入协作 ...

  2. [译]git pull

    git pull把git fetch和git merge压缩成了一条命令. 用法 git pull <remote> 作用和git fetch <remote> &&a ...

  3. 黄学长模拟day1 球的序列

    N个编号为1-n的球,每个球都有唯一的编号.这些球被排成两种序列,分别为A.B序列,现在需要重新寻找一个球的序列l,对于这个子序列l中任意的两个球,要求j,k(j<k),都要求满足lj在A中位置 ...

  4. Apache服务器httpd.exe进程占用cpu超过50%的解决方法

    httpd.exe进程占用cpu超过50%,关闭掉Apache服务,cpu应用率立刻下降到0.  重新启动Apache又出现占用cpu高的情况.  原因是:httpd.exe和防火墙配置有冲突. 解决 ...

  5. JS/HTML 保存图片到本地:HTML <a> download 属性

    JS如何保存图片到本地呢?自己百度一下吧! 这里想要说的是,可以利用 HTML 的 <a> 标签 来是实现保存图片到本地的功能,参考代码如下: <a href="http: ...

  6. 《深入浅出WPF》笔记三

    1.Field:字段,封装在类中的变量. Method:方法,封装在类中的函数. 成员:类中的字段和方法,可分为静态成员和非静态成员. 静态字段在内存中只有一份拷贝. 非静态字段是每个实例拥有一个拷贝 ...

  7. 错误:The Controls collection cannot be modified because the control contains code blocks (i.e. ). .

    用 <%# %>这种写法是写在数据绑定控件中的,之所以用 <%= %>会出现The Controls collection cannot be modified because ...

  8. 在使用开源library的PullToRefreshView中

    下拉刷新几乎是每个应用都会有的功能,且大部分用的都是开源项目,下载地址:下拉刷新.如何在页面刚打开的时候自动触发下拉刷新的呢? 只需要一句代码,在PullToRefreshAdapterView Ba ...

  9. php 通过curl post发送json数据实例

    例1  代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...

  10. 基于SSL协议的双向认证 - SSL协议 [1]

    1  概要说明 在互联网通信方式中,目前用的最广泛的是HTTPS配合SSL和数字证书来保证传输和认证安全了. 2  详细介绍 2.1    HTTPS HTTPS全称:Hypertext Transf ...