【leetcode❤python】 204. Count Primes
#-*- coding: UTF-8 -*-
#Hint1:
#数字i,i的倍数一定不是质数,因此去掉i的倍数,例如5,5*1,5*2,5*3,5*4,5*5都不是质数,应该去掉
#5*1,5*2,5*3,5*4 在数字1,2,3,4的时候都已经剔除过,因此数字5,应该从5*5开始
#Hint2:
#例:
#2 × 6 = 12
#3 × 4 = 12
#4 × 3 = 12
#6 × 2 = 12
#显然4 × 3 = 12,和6 × 2 = 12不应该分析,因为在前两式中已经知道12不是质数,因此如果数字i是质数,i=p*q,p一定小于等于n,因此p小于等于√i
#因此只需循环到√i,就可以了
#AC源码如下:
import math
class Solution(object):
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
isPrime=[True]*n
for i in xrange(2,n):
if i*i>=n:break
if not isPrime[i]:continue
for j in xrange(i*i,n,i):
isPrime[j]=False
count=0
for i in xrange(2,n):
if isPrime[i]:count+=1
return count
【leetcode❤python】 204. Count Primes的更多相关文章
- 【leetcode❤python】 38. Count and Say
#-*- coding: UTF-8 -*- class Solution(object): def countAndSay(self, n): """ ...
- 【刷题-LeetCode】204. Count Primes
Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【LeetCode】 204. Count Primes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...
- 【LeetCode】204 - Count Primes
Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- 【leetcode❤python】 396. Rotate Function
#-*- coding: UTF-8 -*- #超时# lenA=len(A)# maxSum=[]# count=0# while count ...
随机推荐
- RHEL6.5 换源
由于redhat的yum在线更新要收费,即没有注册的无法使用:将redhat的yum卸载.重装第三方源. 第三方源包括:网易,重庆大学,epel 等Author wood_man.2015.10.2 ...
- DNS压力测试工具dnsperf简介
dnsperf是我最近写的一个开源的DNS压力测试工具,用户可以用它来对DNS服务器或者Local DNS做压力测试.dnsperf目前的实现是单进程模式,通过epoll非阻塞地处理网络事件. dns ...
- 用Canvas制作剪纸效果
在做剪纸效果之前,先介绍剪纸效果运用到的一些知识: 1.阴影: 在Canvas之中进行绘制时,可以通过修改绘图环境中的如下4个属性值来指定阴影效果: shadowColor:CSS格式的颜色字串.默认 ...
- flex自适应小例子
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- U家面试prepare: Serialize and Deserialize Tree With Uncertain Children Nodes
Like Leetcode 297, Serialize and Deserialize Binary Tree, the only difference, this is not a binary ...
- vi编辑器的使用
在命令模式下进入编辑模式,输入字母"a","A","i","I","o","O" ...
- Js动态获取iframe子页面的高度////////////////////////zzzz
Js动态获取iframe子页面的高度 Js动态获取iframe子页面的高度总结 问题的缘由 产品有个评论列表引用的是个iframe,高度不固定于是引发这个总结. 方法1:父级页面获取子级页面的高度 ...
- Android隐藏状态栏、导航栏
Android隐藏状态栏.导航栏 private void hideStatusNavigationBar(){ if(Build.VERSION.SDK_INT<16){ this.getWi ...
- 夺命雷公狗-----React---28--小案例之react经典案例todos(全选和反选)完
这个功能实现的步骤如下所示: 最终实现全选和反选,代码如下所示: <!DOCTYPE html> <html lang="en"> <head> ...
- centos安装mono
1.查看mono最新版本 http://download.mono-project.com/sources/mono 2.安装依赖环境 sudo yum install cmake automake ...