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 的素数. 题目很简洁,但是算法实现的 ...
随机推荐
- 很实用的jQuery事件 - toggle() 方法
实例 切换不同的背景色: $("p").toggle( function(){ $("body").css("background-color&quo ...
- 大道至简之编程的精义读后感(Java伪代码)
import.java.大道至简.*; import.java.愚公移山.*; public class YuGongYiShan { 愚公={项目组织者,团队经理,编程人员,技术分析师}: //沟通 ...
- Android开发涉及有点概念&相关知识点(待写)
前言,承接之前的 IOS开发涉及有点概念&相关知识点,这次归纳的是Android开发相关,好废话不说了.. 先声明下,Android开发涉及概念比IOS杂很多,可能有很多都题不到的.. 首先由 ...
- JSON和js对象之间的相互转化
jQuery插件支持的转换方式 $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 http://www. ...
- Mispelling 1510
#include<iostream>#include<string>#include<cstdio>using namespace std;int main(){ ...
- noip2015-day1-t2
题意:有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学.游戏开始时,每人都只知道自己的生日.之后每一轮中, ...
- [SmartFoxServer入门]服务器安装
安装SFS2X: SFS2X平台安装操作和步骤都很简单.我们建议先查看对系统的要求,然后根据你选择的操作系统按照指定的安装向导进行安装. 系统要求: SFS2X是一款支持所有主流操作系统,运行在JVM ...
- canvas放射性渐变填充
今天在学习canvas时,遇到canvas的fillstyle有一个createRadialGradient()方法,创建放射性渐变. 上代码: <!DOCTYPE html> <h ...
- UART的CTS与RTS
在RS232中本来CTS 与RTS 有明确的意义,但自从贺氏(HAYES) 推出了聪明猫(SmartModem)后就有点混淆了.在RS232中RTS 与CTS 是用来半双工模式下的方向切换:HAYES ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...