【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 ...
随机推荐
- JAVASE02-Unit09: 多线程基础
Unit09: 多线程基础 * 线程 * 线程用于并发执行多个任务.感官上像是"同时"执行 * * 创建线程有两种方式. * 方式一: * 继承线程并重写run方法来定义线程要执 ...
- logstash redis kafka传输 haproxy日志
logstash 客户端收集 haproxy tcp日志 input { file { path => "/data/haproxy/logs/haproxy_http.log&qu ...
- EF 保证线程内唯一 上下文的创建
1.ef添加完这个对象,就会自动返回这个对象数据库的内容,比如下面这个表是自增ID 最后打印出来的ID 就是自增的结果 2.lambda 中怎么select * var userInfoList = ...
- lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”
问题 attempt to yield across metamethod/C-call boundary 需求跟如下帖子中描述一致: http://bbs.chinaunix.net/forum.p ...
- Ubuntu16.04配置phpmyadmin
原文 如何安装phpmyadmin-Mysql 数据库管理 使用界面管理器:系统->系统管理->新立得软件包管理器->搜索 phpmyadmin->右键标记安装.或直接使用一条 ...
- TensorFlow安装(Ubuntu 16.04)
原文链接 github not support on this platform pip安装: # Ubuntu/Linux 64-bit $ sudo apt-get install python- ...
- poj 3254 Corn Fields
http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- c++加法高精度算法
c++高精度算法,对于新手来说还是一大挑战,只要克服它,你就开启了编程的新篇章,算法. 我发的这个代码并不是很好,占用内存很多而且运行时间很长(不超过1秒),但是很好理解,很适合新手 高精算法的本质就 ...
- linux系统无法启动解决方案
windows和linux双系统一般先安装Windows,分两个主分区,把Linux安装在另外的主分区上.Linux编译内核,添加NTFS分区支持,然后Mount NTFS 分区即可访问Windows ...
- 浅述WinForm多线程编程与Control.Invoke的应用
VS2008.C#3.0在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在W ...