#-*- 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的更多相关文章

  1. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  2. 【刷题-LeetCode】204. Count Primes

    Count Primes Count the number of prime numbers less than a non-negative number, *n*. Example: Input: ...

  3. 【leetcode❤python】Sum Of Two Number

    #-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...

  4. 【LeetCode】 204. Count Primes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 素数筛法 参考资料 日期 [LeetCode] 题目 ...

  5. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  6. 【leetcode❤python】 8. String to Integer (atoi)

    #-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...

  7. 【leetcode❤python】 1. Two Sum

    #-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object):    def twoSum(self, nums, target):  ...

  8. 【leetcode❤python】 58. Length of Last Word

    #-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object):   ...

  9. 【leetcode❤python】 396. Rotate Function

    #-*- coding: UTF-8 -*- #超时#        lenA=len(A)#        maxSum=[]#        count=0#        while count ...

随机推荐

  1. 使用HttpClient连接池进行https单双向验证

    https单双向验证环境的搭建参见:http://www.cnblogs.com/YDDMAX/p/5368404.html 一.单向握手 示例程序: package com.ydd.study.he ...

  2. PostgreSQL ROW_NUMBER() OVER()

    转自:http://blog.csdn.net/luojinbai/article/details/45078809 语法:  ROW_NUMBER() OVER( [ PRITITION BY co ...

  3. 理解会话Session

  4. SpringSecurity操作指南-在SpringMVC项目上配置Spring Security

  5. 使用TreeView 使用多选功能

    1.要用TreeView多选就要显示复选框,TreeView默认不显示复选框,显示复选框: TreeView2.ShowCheckBoxes = TreeNodeTypes.All; 初始化TreeV ...

  6. python fabric install

    1,install setuptools 2,install pip 3,install pycrpto 4,install paramiko 5,install fabric

  7. Css定位之relative_慕课网课程笔记

    前言 最近在慕课网上跟着张鑫旭大神重新学习一遍CSS相关的知识,以下是学习的笔记以及个人一些理解 relative对绝对定位的限制 1.限制绝对定位 绝对定位的top.left.right和botto ...

  8. 自己写一个swap函数交换任意两个相同类型元素的值 对空指针的使用 字节大小的判断(二)了解原理

    验证的代码: #include <stdio.h> int main(){ char c = 'z'; ) + (c << ) + () + 'a'; printf(" ...

  9. 通过jxl 读取excel 文件中的日期,并计算时间间隔

    java读取excel里面的日期会出现相差8小时的问题. 比如excel里面有一个日期是:2012-7-2 17:14:03秒,用Cell cell=readSheet.getCell(colNo, ...

  10. final review 报告

    项目名:约跑 组名:nice! 组长:李权 组员:刘芳芳于淼韩媛媛 宫丽君 final Review会议 时间:2016.12.2  代码git的地址:https://git.coding.net/m ...