204. Count Primes 统计<n的质数的个数
[抄题]:
Count the number of prime numbers less than a non-negative number, n.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
[一句话思路]:
质数的倍数是合数
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
质数的倍数是合数
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int countPrimes(int n) {
//cc
if (n < 2) {
return 0;
} //ini
int count = 0;//prime num
int[] notPrime = new int[n];//not prime num //for i, i * j
for (int i = 2; i < n; i++) {
if (notPrime[i] == 0) count++;
for (int j = 2; i * j < n; j++) {
notPrime[i * j] = 1;
}
} return count;
}
}
204. Count Primes 统计<n的质数的个数的更多相关文章
- [leetcode] 204. Count Primes 统计小于非负整数n的素数的个数
题目大意 https://leetcode.com/problems/count-primes/description/ 204. Count Primes Count the number of p ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 204. Count Primes - LeetCode
Queston 204. Count Primes Solution 题目大意:给一个数,求小于这个数的素数的个数 思路:初始化一个boolean数组,初始设置为true,先遍历将2的倍数设置为fal ...
- [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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- [LeetCode] 204. Count Primes 解题思路
Count the number of prime numbers less than a non-negative number, n. 问题:找出所有小于 n 的素数. 题目很简洁,但是算法实现的 ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- LeetCode 204. Count Primes计数质数 (C++)
题目: Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: ...
- [LeetCode] 204. Count Primes 计数质数
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
随机推荐
- 在struts2.5版本中使用DMI遇到问题
struts2.5 为了提升安全性,添加了 allomethod 这么个玩意. 解决方法是在配置文件中添加: <package name="exam" extends=&qu ...
- SpringMVC_总结_03_SpringMVC相关注解
一.前言 在前面的小节中,我们配置了注解驱动和自动扫描包,然后就可以使用SpringMVC相关注解了. 二.@Controller @Controller用来修饰类,源码如下: package org ...
- Git_学习_01_ git 安装与配置
参考:windows下Git BASH安装 二.参考资料 1. windows下Git BASH安装
- stencil in unity3d
Pass { Stencil { Ref Comp Always Pass REPLACE } AlphaTest Greater Blend SrcAlpha OneMinusSrcAlpha Co ...
- mysql 开发标准规范
一.表设计 1. 库名.表名.字段名使用小写字母,“_”分割. 2. 库名.表名.字段名不超过12个字符. 3. 库名.表名.字段名见名知意,尽量使用名词而不是动词. 4. 优先使用InnoDB存储引 ...
- C语言编译全过程
编译的概念:编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序. ...
- 让camera实现类似cs第一人称视角旋转和位移
直接把这个脚本挂在摄像机上就可: using System.Collections; using System.Collections.Generic; using UnityEngine; /* * ...
- 【ASP.NET Web API2】利用HttpClient调用Web API(TODO)
参照: 在一个空ASP.NET Web项目上创建一个ASP.NET Web API 2.0应用 纯属记录一下遇到的问题: 我们利用HttpClient来调用自宿主方式寄宿的Web API.HttpCl ...
- UDP10040 和 setsockopt设置大全
今天无意之中碰到 UDP 10040 错误 原来是缓冲区不够,以下转载的解决方法以供不时之需. 1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该sock ...
- WPF案例:如何设计搜索框(自定义控件的原则和方法)
我们平时自定义WPF控件的方法有:Style,DataTemplate,ControlTemplate, DependencyProperty, CustomControl等几个方法. 按照优先顺序应 ...