Leetcode 204 Count Primes 数论
题意:统计小于n的质数个数。
作为一个无节操的楼主,表示用了素数筛法,并没有用线性素数筛法。
是的,素数筛法并不是该题最佳的解法,线性素数筛法才是。
至于什么是素数筛法,请百度吧。
class Solution {
public:
int countPrimes(int n) {
bool *isp= new bool[n];
for (int i = ; i < n; ++i)
{
isp[i]= true;
}
for (int i = ; i * i< n; ++i)//素数筛法
{
if(isp[i]){
for(int j = i + i; j < n; j+=i){
isp[j] = false;
}
}
}
int ans = ;
for (int i = ; i < n; ++i){
ans += isp[i];
}
delete isp;
return ans;
}
};
Leetcode 204 Count Primes 数论的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- [LeetCode] 204. Count Primes 质数的个数
Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Java [Leetcode 204]Count Primes
题目描述: Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: Let's ...
- LeetCode 204. Count Primes (质数的个数)
Description: Count the number of prime numbers less than a non-negative number, n. 题目标签:Hash Table 题 ...
- LeetCode 204 Count Primes
Problem: Count the number of prime numbers less than a non-negative number, n. Summary: 判断小于某非负数n的质数 ...
- Java for LeetCode 204 Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...
- (easy)LeetCode 204.Count Primes
Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
随机推荐
- zzuliOJ 1904小火山的股票交易;
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; # ...
- DB2 UDB DBA 核对清单
本文摘自 http://www-128.ibm.com/developerworks/cn/db2/library/techarticles/dm-0404snow/index.htmlDB2 UDB ...
- PHP-redis中文文档-命令
关于redis of php的安装,详见自己的为知笔记,其中包含: 1.php版本的redis的安装 2.redis的使用方法 3.redis带conf文件的启动 这里介绍redis的命令,php版本 ...
- Sql Server 复制表
SELECT INTO 和 INSERT INTO SELECT 两种表复制语句 1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,fiel ...
- [Ng]Angular应用点概览
1. 使用模块化写法. var app = angular.module('myApp', []); app.controller('TextController', function($scop ...
- (转)combogrid的代码实例
EasyUI中combogrid的代码实例 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...
- 1296: [SCOI2009]粉刷匠
Description windy有 N 条木板需要被粉刷. 每条木板被分为 M 个格子. 每个格子要被刷成红色或蓝色. windy每次粉刷,只能选择一条木板上一段连续的格子,然后涂上一种颜色. 每个 ...
- jsp_设置文件编码
jsp有两种方法可以设置文件编码: (1)<%@page language="java" contentType="text/html;charset=utf-8& ...
- git flow的安装和使用
确保安装了git 1.windows系统下安装 进入cmd clone github上的gitflow到一个文件夹下 我这里clone到 c:\gitflow git clone git://gith ...
- 改变tableView索引颜色
_tableView.sectionIndexBackgroundColor = [UIColor clearColor]; _tableView.sectionIndexColor ...