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. AngularJS 国际化——Angular-translate

    对于一个用户群面向全球的的应用来说,不得不考虑国际化的问题.当然,即便是刚刚起步的小应用,如果有心搞大,也应该提前设计国际化的方案. 本篇讲述使用AngularJS构建的应用的简单国际化方案,准确的说 ...

  2. nyoj 252 01串 动态规划( java)

    当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...

  3. 关于QT写注册表开机自启动

    注册表中权限: 1.HKEY_CURRENT_USER 2.HKEY_LOCAL_MACHINE 网上有很多帖子都是用的2,其实这样有违用户权限,而且如果不是管理员用户,会写入不成功! 代码如下: Q ...

  4. html 常用标签补充

    <body> <!--预处理标签 <pre>--> <pre> 你好, 空格 换3行. 你<sup>上标</sup>好<s ...

  5. 定时任务-在spring中配置quartz

    使用的版本Spring4.04+Quartz2.2.3,关于jar包自行下载. 详细需要以下几个步骤来完成: 1.  定义要执行的Job类 2.  定义quartz的配置文件applicationCo ...

  6. Lab1--关于安装JUnit的简要描述

    安装JUnit的过程描述: 下载两个jar包: hamcrest-all-1.3.jar junit-4.12.jar 注意在导入完成jar包之后不要随意改变jar包的路径. 创建java程序,书写如 ...

  7. Oracle添加数据报文字与格式字符串不匹配错误

    今天在学习Oracle时碰到一个错:文字与格式字符串不匹配. 我在Oracle数据库中创建了一张表: --创建员工表employee create table employee ( empon ) n ...

  8. Triangle leetcode

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. 使用C++扩展Python的功能 转自:http://blog.csdn.net/magictong/article/details/8897568#comments

    使用C++扩展Python的功能 环境 VS2005Python2.5.4 Windows7(32位) 简介 长话短说,这里说的扩展Python功能与直接用其它语言写一个动态链接库,然后让Python ...

  10. 快速排序python实现

    #--×--coding:utf-8-*- def main(): nlist = [] while 1: tmp = raw_input("Please input your elemen ...