码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode 204] Count Primes

时间:2015-04-28 23:02:38      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:java   算法   leetcode   

题目链接:count-primes


Description:

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


public class Solution {
	public int countPrimes(int n) {
		if(n <= 2) return 0;
		List<Integer> primes = new ArrayList<Integer>();
		primes.add(2);
		for (int i = 3; i < n; i += 2) {
			int sqrt_i = (int) Math.sqrt(i);
			for (int j = 0; i % primes.get(j) != 0; j++) {
				if (primes.get(j) > sqrt_i) {
					primes.add(i);
					break;
				}
			}
		}
		return primes.size();
    }
}


[LeetCode 204] Count Primes

标签:java   算法   leetcode   

原文地址:http://blog.csdn.net/ever223/article/details/45341267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!