2019独角兽企业重金招聘Python工程师标准>>>

Count the number of prime numbers less than a non-negative number, n.

Example:

Input: 10
Output: 4
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7.
//想先用粗暴的方法实现一下
public static int countPrimes(int n) {int count = 0;for(int i = 1;i<n;i++){if(isPrime(i)){count ++;}}return count;}//全部判断一下是否书素数 但是这看到有是那个循环 时间复杂度接受不了
private static boolean isPrime(int i) {if(i ==1){return false;}for(int j =2;j< i;j++){for(int j2 =2;j2< i;j2++){if(j*j2 == i){return false;}}}return true;}//如果稍微优化一下呢 结果还是超时
public static boolean isPrime2(int i) {if(i ==1 ){return false;}//这样是否可以快点去掉一些数据if(i > 10 && (i%2==0 ||i%3==0||i%5==0||i%7==0)){return false;}for(int j =2;j< Math.sqrt(i)+1;j++){int j2 = (int)(Math.sqrt(i)) > 2?  (int)(Math.sqrt(i)):2;for(; j2< i; j2++){if(j*j2 == i){return false;}}}return true;}/**这里的想法是反一下 找出所有的合数*/
public static int countPrimes2(int n) {boolean[] notPrime = new boolean[n];int count = 0;for (int i = 2; i < n; i++) {if (notPrime[i] == false) {count++;for (int j = 2; i*j < n; j++) {notPrime[i*j] = true;}}}return count;
}//这里用了不断累加的方法 感觉也不错
public static  int countPrimes3(int n) {boolean[] m = new boolean[n];int count = 0;for (int i=2; i<n; i++) {if (m[i]) {continue;}count++;for (int j=i; j<n; j=j+i) {m[j] = true;}}return count;}

git:https://github.com/woshiyexinjie/leetcode-xin/tree/master/src/main/java/com/helloxin/leetcode/algorithms

转载于:https://my.oschina.net/u/2277632/blog/2962468

Count Primes(leetcode204)相关推荐

  1. Leetcode-204 Count Primes

    #204 Count Primes Count the number of prime numbers less than a non-negative number, n. 题解:这道题如果对每个小 ...

  2. 204. Count Primes

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  3. leetCode 204. Count Primes 哈希 求素数

    204. Count Primes 求素数 Description: Count the number of prime numbers less than a non-negative number ...

  4. 【LeetCode 剑指offer刷题】特殊数题3:204 Count Primes

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 204. Count Primes Count the number of prime numbers less t ...

  5. [LeetCode] 204. Count Primes

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

  6. LeetCode204——count primes

    方法还是有很多的,通过率很低,不过这题还是需要思考清楚,不然很容易t class Solution { public:int countPrimes(int n){if(n == 0 || n == ...

  7. [LeetCode] Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n 解题思路 采用Eratosthene ...

  8. LeetCode Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 题意:给出一个数n,求小于n的素数 ...

  9. [LeetCode] Count Primes - 素数系列问题

    题目概述: Description: Count the number of prime numbers less than a non-negative number, n. 解题方法: 题意是给出 ...

最新文章

  1. 软件测试2019:第四次作业—— 性能测试(含JMeter实验)
  2. [恢]hdu 1860
  3. java动物乐园_基于jsp的动物园管理系统-JavaEE实现动物园管理系统 - java项目源码...
  4. 洛谷P3006 [USACO11JAN]瓶颈Bottleneck(堆模拟)
  5. Java代码质量检测评估工具-Findbugs
  6. 吴恩达机器学习作业(五):支持向量机
  7. oracle 根据分隔符提取,oracle自定义函数按照某个分隔符拆分字符串
  8. 玩转python(2)多线程的历史2
  9. 厦门大学计算机学硕复试,【图片】一战厦大计算机上岸,经验帖。慢更【考研吧】_百度贴吧...
  10. java面向对象的理解_面向对象及其核心的概念:抽象、继承与多态、封装
  11. Ansible详解(十一)——Ansible Template高级控制
  12. Python-编码格式
  13. 板簧的弹性系数如何计算_滑板式钢板弹簧悬架变刚度计算方法的研究
  14. js 正则匹配两个字符串中间的字符,一级匹配带有反斜杠的字符串
  15. STM32f103系列各个型号芯片之间的程序移植
  16. 美国伊利诺伊大学香槟分校计算机专业,伊利诺伊大学香槟分校
  17. 沈阳贫民窟男孩的5条择偶观
  18. [转帖]三星F488E的JAVA安装方法
  19. 计算机的硬件系统和软件系统
  20. oracle utl_file权限,Oracle内建包UTL_FILE使用说明

热门文章

  1. c#抓取別的網頁的內容
  2. 肿瘤表观遗传相关重磅数据库:MR4Cancer使用指南
  3. JavaWeb(八)——JSP(Java服务器端页面)
  4. 计算机视觉与深度学习 | 基于多源传感器数据融合的动态场景SLAM研究
  5. 画出18*18的棋盘以及用不同颜色绘制出同心圆(python实现)
  6. Visual Studio原生开发的20条调试技巧
  7. 含根式的定积分计算_不定积分计算法则总结
  8. netcore部署到docker 实现excel生成_Docker部署Redis集群----第七节(docker-redis-sentinel集群实现篇)...
  9. docker 查看已安装容器_WIN7下安装Docker容器
  10. python3精要(6)-数字,分数,除法