【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 <= ) 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)的更多相关文章
- 【leetcode】Happy Number(easy)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【leetcode】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 【leetcode】Remove Element (easy)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【LeetCode】Reconstruct Itinerary(332)
1. Description Given a list of airline tickets represented by pairs of departure and arrival airport ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 【LeetCode】Counting Bits(338)
1. Description Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num ...
随机推荐
- 用一个简单的例子来理解python高阶函数
============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...
- [译]Create a Web API in MVC 6
原文: http://www.asp.net/vnext/overview/aspnet-vnext/create-a-web-api-with-mvc-6 ASP.NET 5.0的一个目标是合并MV ...
- [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist
This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...
- 【转】GATK使用方法详解(包含bwa使用)
一.使用GATK前须知事项: (1)对GATK的测试主要使用的是人类全基因组和外显子组的测序数据,而且全部是基于illumina数据格式,目前还没有提供其他格式文件(如Ion Torrent)或者实验 ...
- 输入三个数a,b,c,要示按由小到大的顺序输出
#include<stdio.h>int main(){ double a,b,c,t; scanf("%lf %lf %lf",&a, ...
- loadrunner-27796错误寻求解决办法
Action.c(58): Error -27796: Failed to connect to server "www.baidu.com:80": [10048] Addres ...
- java之String
一.构造器 package com.string; import java.io.UnsupportedEncodingException; import java.nio.charset.Chars ...
- springMVC之配置
1.项目结构 2.所需jar包 3.web.xml文件 <?xml version="1.0" encoding="UTF-8"?> <web ...
- iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告
一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...
- iOS开发——UI进阶篇(三)自定义不等高cell,如何拿到cell的行高,自动计算cell高度,(有配图,无配图)微博案例
一.纯代码自定义不等高cell 废话不多说,直接来看下面这个例子先来看下微博的最终效果 首先创建一个继承UITableViewController的控制器@interface ViewControll ...