LeetCode 204. Count Primes
2016-07-09 16:36
387 查看
Problem: https://leetcode.com/problems/count-primes/
Count the number of prime numbers less than a non-negative number, n.
Thought:
SieveofEratosthenes
Code C++:(meet Runtime Error on test case 150000, which I haven't found the reason)
Count the number of prime numbers less than a non-negative number, n.
Thought:
SieveofEratosthenes
Code C++:(meet Runtime Error on test case 150000, which I haven't found the reason)
class Solution { public: int countPrimes(int n) { if (n <= 1) { return 0; } int nums ; for (int i = 0; i < n; i++) { nums[i] = 1; } nums[0] = 0;nums[1] = 0; for (int i = 2; i * i <= n; i++) { if (!nums[i]) { continue; } for (int j = 2; i * j < n; j++) { nums[i * j] = 0; } } int solve = 0; for (int i = 0; i < n; i++) { solve += nums[i]; } return solve; } };
相关文章推荐
- 【Cocos2d-x 3.0学习笔记】 AnchorPoint 和Position 关系
- 错误:E/AndroidRuntime(6539): Caused by: java.lang.ClassCastException: android.widget.
- SNMP
- 三维重建(三)相机参数标定与光束平差法(Bundle Adjustment)
- Java动态代理学习
- Android sqlite联合主键的使用
- Java静态代理学习
- LightOJ1017 Brush (III)(DP)
- python学习——函数
- 利用window.open实现post方式的参数传递
- 107----ORACLE日期时间函数大全
- 编程开发经验谈:其实Unix很简单
- androidstudio提示adb错误:cannot parse version string:kg01的解决方法
- androidstudio提示adb错误:cannot parse version string:kg01的解决方法
- 只卖一本书的森冈书店
- 第15周 啊哈算法 链表:用指针链表和数组模拟链表
- 【JZOJ 4598】准备食物
- Android Studio项目目录结构介绍
- 106---oracle的PL/SQL,异常处理, 游标(CURSOR), 子程序,存储过程
- mysql开启查询日志 操作日志 错误日志 慢查询日志