Leetcode(59)-Count Primes
题目:
Description:
Count the number of prime numbers less than a non-negative number, n.
思路:
- 题意:求小于给定非负数n的质数个数
西元前250年,希腊数学家厄拉多塞(Eeatosthese)想到了一个非常美妙的质数筛法,减少了逐一检查每个数的的步骤,可以比较简单的从一大堆数字之中,筛选出质数来,这方法被称作厄拉多塞筛法(Sieve of Eeatosthese)。
具体操作:先将 2~n 的各个数放入表中,然后在2的上面画一个圆圈,然后划去2的其他倍数;第一个既未画圈又没有被划去的数是3,将它画圈,再划去3的其他倍数;现在既未画圈又没有被划去的第一个数 是5,将它画圈,并划去5的其他倍数……依次类推,一直到所有小于或等于 n 的各数都画了圈或划去为止。这时,表中画了圈的以及未划去的那些数正好就是小于 n 的素数。
代码:
public class Solution {
public int countPrimes(int n) {
if(n < 3){
return 0;
}
boolean[] prime =new boolean[n];
prime[2] = false;
for(int i = 3;i < n;i++){
if(i % 2 == 0){
prime[i] = true;
}else{
prime[i] = false;
}
}
for(int a = 3;a < n;a= a+2){
if(!prime[a]){
if(a * a < n){
for(int j = 2;a * j < n;j++){
prime[a*j] = true;
}
}
}
}
int count = 0;
for(int b = 2;b < n;b++){
if(!prime[b]){
count++;
}
}
return count;
}
}
Leetcode(59)-Count Primes的更多相关文章
- Leetcode(204) Count Primes
题目 Description: Count the number of prime numbers less than a non-negative number, n. Credits: Speci ...
- LeetCode(38) Count and Say
题目 The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111 ...
- LeetCode(59):螺旋矩阵 II
Medium! 题目描述: 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵. 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, ...
- LeetCode(59)Length of Last Word
题目 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return th ...
- LeetCode(59)SPiral Matrix II
题目 Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...
- Leetcode(5)最长回文子串
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 ...
- Qt 学习之路 2(59):使用流处理 XML
Qt 学习之路 2(59):使用流处理 XML 豆子 2013年7月25日 Qt 学习之路 2 18条评论 本章开始我们将了解到如何使用 Qt 处理 XML 格式的文档. XML(eXtensible ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
随机推荐
- jvm java虚拟机 新生代的配置
1.1.1.1. -Xmn参数 参数-Xmn1m可以用于设置新生代的大小.设置一个较大的新生代会影响老生代的大小,因为这两者的总和是一定的,这个系统参数对于系统性能以及GC行为有很大的影响,新生代一般 ...
- 剑指Offer——回溯算法解迷宫问题(java版)
剑指Offer--回溯算法解迷宫问题(java版) 以一个M×N的长方阵表示迷宫,0和1分别表示迷宫中的通路和障碍.设计程序,对任意设定的迷宫,求出从入口到出口的所有通路. 下面我们来详细讲一 ...
- Android初级教程:shape的基本用法
转载本文请注明出处:http://blog.csdn.net/qq_32059827/article/details/52203347 点击打开链接 在自定义进度条之前,先来学习一下shape的用 ...
- 随机采样和随机模拟:吉布斯采样Gibbs Sampling实现高斯分布参数推断
http://blog.csdn.net/pipisorry/article/details/51539739 吉布斯采样的实现问题 本文主要说明如何通过吉布斯采样来采样截断多维高斯分布的参数(已知一 ...
- Android之Notification-android学习之旅(二)
notification常用于下拉式的消息推送. Notification的构成 Nitification的实例 1.新建一个Builder,要选Notification.compat包. 2.然后用 ...
- love~LBJ,奥布莱恩神杯3
时间:2016年6月20日8:00:地点:美国金州甲骨文(Oracle)球馆:事件:G7大战,又一次见证伟大赛季奥布莱恩神杯得主--金州勇士VS克利夫兰骑士...... 可以说,这是NBA以来 ...
- How to Find the Self Service Related File Location and Versions
How to Find the Self Service Related File Location and Versions (文档 ID 781385.1) In this Document ...
- leetcode:程序员面试技巧
起因 写在开头,脑袋铁定秀逗了,历时20多天,刷完了leetcode上面151道题目(当然很多是google的),感觉自己对算法和数据结构算是入门了,但仍然还有很多不清楚的地方,于是有了对于每道题目写 ...
- 【UNIX网络编程第三版】阅读笔记(一):代码环境搭建
粗略的阅读过<TCP/IP详解>和<计算机网络(第五版)>后,开始啃这本<UNIX网络编程卷一:套接字联网API>,目前linux下的编程不算太了解,在阅读的过程中 ...
- 高通QSD MSM APQ区别
高通msm是Mobile Station Modem 的缩写,即移动基带工作站,是指带有基带芯片的移动处理器,实际就是基带内置的手机处理器(soc)系列. qsd是qualcomm snapdrago ...