【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 ...
随机推荐
- Runtime实战之定制TabBarItem大小
方案一:UIEdgeInsets 适用场景: 适合APP的TabBarItemImage的图片资源放在本地 图片超出tabbar的高度,需移动其位置,来进行适应 弊端: 若在本地配置好后,tabbar ...
- C语言:使用命令行参数用字符串读取流和输出流进行文本文件的复制
#include<stdio.h> int main(int argc,char *argv[]) { //检查用户的参数是否正确 if(argc<3) { printf(" ...
- magento 安装
magento 安装其实很简单. 第一步,打开,你下载好的程序,找到php.ini.simple,根据这里面的的要求,来修改,你本地或服务器 php.ini的配置. 第二步,开始安装了(注意,先在你的 ...
- I’m Sure It Will Only Take You A Few Days To Code
from:http://danshipper.com/non-technical-people-cant-estimate-developmen “So the site’s pretty simpl ...
- poj 2251 Dungeon Master
http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- WSF脚本详解:组合JS和VBS代码
1.概述 Windows Script Host除了提供一个对象模型之外,还提供了一种脚本框架,这就是WSF脚本.通过WSF约定的标记元素,可以将多种脚本语言写的代码块组合起来,完成任务.除此之外,还 ...
- C# byte数组与Image的相互转换
功能需求: 1.把一张图片(png bmp jpeg bmp gif)转换为byte数组存放到数据库. 2.把从数据库读取的byte数组转换为Image对象,赋值给相应的控件显示. 3.从图片byte ...
- 【原创】如何用Android Studio断点安卓自带Service或Bind类型的Service
很久以来,我一直想找一种方法来断点调试安卓系统自身的Service,或者bind类型的Service,比如我想看WifiManager里面的getWifiApConfiguration函数是如何实现的 ...
- scrapy的scrapyd使用方法
一直以来,很多人疑惑scrapy提供的scrapyd该怎么用,于我也是.自己在实际项目中只是使用scrapy crawl spider,用python来写一个多进程启动,还用一个shell脚本来监控进 ...
- 苹果版App开发心得
这几个月中做的工作包括网站开发.安卓App开发和苹果App开发,前两者用的语言都是我熟悉的java,故苹果知识的学习,较安卓知识的学习,多出「语言基础」一块,其他方面差不多. 之前发过安卓那篇,如感兴 ...