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. 大熊君大话NodeJS之------FS文件模块

    一,开篇分析 文件系统模块是一个简单包装的标准 POSIX 文件 I/O 操作方法集.可以通过调用 require("fs") 来获取该模块.文件系统模块中的所有方法均有异步和同步 ...

  2. QT共享库的创建与调用(初级)(附:UI界面不能被改变的其中一个原因)

    背景: 最近在做的一个项目其中一部分既是实现PC与下位机的USB通信.windows平台下已经完成,现需移植到linux平台下. 在linux系统中,通过一段时间的工作,设备已被配置成hid类(后续再 ...

  3. Linux之服务器时间同步

    Linux时间同步(把nameNode1作为时间同步服务器) 设置时区 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 联网情况:ntpdate ...

  4. VMware 关闭虚拟机 Ubuntu 12 的 3D 效果,提高性能

    Ubuntu 2012,有 2D 和 3D 的渲染效果,但是 在虚拟机中,开启 3D 效果后,特别卡.好在 VMware 中有个“关闭3D”的开关,如下图所示: 去掉勾选“加速 3D 图形”

  5. 汉字转拼音(pinyin4j)

    1.引入依赖 <dependency> <groupId>pinyin4j.sourceforge.net</groupId> <artifactId> ...

  6. 超级强大的formValidator

    来源: http://www.cnblogs.com/wzmaodong http://www.neatstudio.com/show-73-1.shtml  (全) http://www.cnblo ...

  7. HDU 5038 Grade北京赛区网赛1005

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5038 解题报告:就是输入n个数w,定义s = 10000 - (100 - w)^2,问s出现频率最高 ...

  8. BZOJ1367——[Baltic2004]sequence

    1.题目大意:给一个序列t,然后求一个序列z,使得$|z1-t1|+|z2-t2|+...+|zn-tn|$的值最小,我们只需要求出这个值就可以了,并且z序列是递增的 2.分析:这道题z序列是递增的, ...

  9. FTP 传输中的主动模式和被动模式

    最近做一个项目用到FTP和其它系统进行文件传输,结果在FTP网络连接的问题上花了很多时间,由于太久没搞多FTP,忘记了FTP不单单开放21端口,客户端采用不同连接模式对网络有不同.在此重温一下FTP的 ...

  10. 怎样在myEclipse中使用debug调试程序?

    怎样在myEclipse中使用debug调试程序? 最基本的操作是:     1.首先在一个java文件中设断点,然后debug as-->open debug Dialog,然后在对话框中选类 ...