Leetcode-204. Count Primes
来源:互联网 发布:手机虚拟摇杆软件 编辑:程序博客网 时间:2024/06/10 01:13
前言:为了后续的实习面试,开始疯狂刷题,非常欢迎志同道合的朋友一起交流。因为时间比较紧张,目前的规划是先过一遍,写出能想到的最优算法,第二遍再考虑最优或者较优的方法。如有错误欢迎指正。博主首发CSDN,mcf171专栏。
博客链接:mcf171的博客
——————————————————————————————
Description:
Count the number of prime numbers less than a non-negative number, n.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
Hint:
Let's start with a isPrime function. To determine if a number is prime, we need to check if it is not divisible by any number less than n. The runtime complexity of isPrime function would be O(n) and hence counting the total prime numbers up to n would be O(n2). Could we do better?
public class Solution { public int countPrimes(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; }}
0 0
- 【LeetCode】204.Count Primes
- [leetcode] 204.Count Primes
- LeetCode 204. Count Primes
- Leetcode 204. Count Primes
- [LeetCode]204. Count Primes
- 204. Count Primes LeetCode
- LeetCode *** 204. Count Primes
- LeetCode 204. Count Primes
- leetcode-204. Count Primes
- LeetCode-204.Count Primes
- Leetcode 204. Count Primes
- 【leetcode】204. Count Primes
- [Leetcode]204. Count Primes
- LeetCode 204. Count Primes
- [leetcode] 204. Count Primes
- 204.[LeetCode]count primes
- [LeetCode] 204. Count Primes
- Leetcode 204. Count Primes
- 寻找将图片一次可以做成android里面各个分辨率的工具
- 求助如何做一个在屏幕底部要突出来的按钮
- LBS教程---如何使用Android SDK进行开发
- 关于IOS下多核编程问题OpenMp(高分)
- ZCMU-1417-快速幂
- Leetcode-204. Count Primes
- Kali 2016版本下增强工具open-vm-tools的安装
- 关于联合登陆获取cookie值
- 红包外挂那点事
- EditText或者TextView单击时弹出DatePicker对话框,然后弹出软键盘,后来我禁止EditText还是弹出来
- listview 跟 addTextChangedListener冲突
- 快学Scala(6) 对象
- ios 写日历时 collectionview的cell的复用问题
- DNSChanger卷土重来,家用路由器当心了