解决一道leetcode算法题的曲折过程及引发的思考

时间:2021-01-21 05:02:10

写在前面

本题实际解题过程是 从 40秒 --> 24秒 -->1.5秒 --> 715ms --> 320ms --> 48ms --> 36ms --> 28ms

最后以28ms的运行速度告结, 有更好的解法或者减少算法复杂度的博友可以给我发消息也可以评论, 我们一起讨论.


第一次在leetcode解算法题,想来个开门红,先挑选一道简单的题目AC了再说。leetcode对每道题都有一个难度提示,分别是easy,medium,hard

于是在第一页中看到了几道标着easy的题目,突然看到一道easy题目的通过率只有 18.6%,也是挺低的,在好奇心的驱使下点了进去。

题目的大概意思是:

  给定一个数值n,算出从0到n之间有多少个数是质数

这种题目的通过率只有18.6%?看到题目后我就觉得太简单了,明显是水题啊,于是着手做了起来,结果历经曲折,以下是我的四次尝试过程……

examination questions


Count Primes

Description:

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

Please use the following function to solve the problem:

int countPrimes(int n){

}


第一次尝试解决

我的思路:先了解质数的判断定义:除了1和它本身外,不能被其他自然数整除。从这个判断定义上来看,用编程实现很简单。于是……

转换成编程思想:

The first step:给定一个n,对于大于2的数,从2开始,通过for循环,依次判断是否为质数,直到n-1

the second step:对每个要判断是否为质数的数,再进行从2到(自身-1)直接的数进行for循环遍历检查,一旦发现可以被某个数整除,则跳出,该数不是质数,进行下一个数的检查。

代码实现:

#include <stdio.h>

int countPrimes(int n) {
if (n == || n == || n == ){
printf("0\n");
return ;
}
int temp = ;
int flag = ;
for (int i = ; i < n; i++){
for (int j = ; j < i; j++){
if (i%j == ){
flag = ;
break;
}
}
if (flag == ){
temp++;
}
else{
flag = ;
}
} printf("%d\n",temp);
return ;
} int main(){
int n;
scanf("%d",&n);
countPrimes(n); return ;
}

自己找了几个值测试了一下

测试如下:

输入54 正确结果是16

结果正确

输入174 正确结果是 40

结果正确

都正确,这么简单的题?

于是赶紧去提交答案

结果显示:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALsAAAAiCAIAAACImz+2AAACH0lEQVR4nO2Zy42DMBRFpzZEKSidQCGwogHWSBRAA9BAOmA3Cxts/MPXmGRGukes4u/Dx89G+dkIQfj59gTIP4PGEAwaQzBoDMGgMQSDxhAMGkMwaAzBoDEEg8YQDBpDMGgMwUCNWeZX0ZfW09RTWfRlvead3dIOZdG/2rdZMILDmfXXpphGXzXjqeYldf53GSd3+E+0iiWXMY7XnwGvMbdYm6IvaUwiqaeSuZbaJh7rviz6pt3dqtdte3eVWAB9nY4fPesXl2Oihjvqn403Rfe/azETmaVkJ3vnep+ntBcI0F+keptGcz5prfLymDHeR75TsdH1Z+isvRxvzMVwt43RM5N0VLS1k+51gP4iVwrf55PWKjvPGVOvRzW5IUSdal7s5iJm614CGBMc7nyPwU4lI8ZXpSUbI/doPQcCvCzae5MZRdRMa/UAz51Ko/m7DLKaF19isG4MwKkUHC6PMWraqu1Yu7NjIMDLIpX5tJyX1uoBPmKMrPYpY+zhIGP87/q4RihFzNXaTr/TGMVNYyI/gv6UMWIy8oq9y+06EYZuCQV4WRR1KsW1eoAvGeO6xzmC3O8l1nUvgzGu3OD8uhZu+XQHbr6BO6y/t7utsvM1YzbjFYR2T2ZjVLfmoF5j5MbdDRMzd337nA7WQID+IrX8Q9caOS+tVV74LwHBoDEEg8YQDBpDMGgMwaAxBIPGEAwaQzBoDMGgMQSDxhCMXz8bWKb+xM4ZAAAAAElFTkSuQmCC" alt="" />

失败!

用的时间太长了,测试值是49969

于是我用49969测试了一下,结果用了40秒才运算完成,这也太久了,肯定通不过,想到之前看到的通过率,我想我大概知道这道题其实没那么简单,leetcode也不可能水。

第二次尝试解决

我知道那个for循环,如果数值非常大的话,那个运算量会很大,而且测试了很多不可能的值,比如 n = 40,他不可能被 大于 40/2 ~ 40之间的数整除吧,也就是从21到40之间的数值可以直接去掉,不用计算了,然后我就去掉这一部分,代码修改如下:

for (int j = ; j < i; j++){
if (i%j == ){
flag = ;
break;
}
}

改成:

for (int j = ; j < i/; j++){
if (i%j == ){
flag = ;
break;
}
}

完整代码:

#include <stdio.h>

int countPrimes(int n) {
if (n == || n == || n == ){
printf("0\n");
return ;
}
int temp = ;
int flag = ;
for (int i = ; i < n; i++){
for (int j = ; j < i/; j++){
if (i%j == ){
flag = ;
break;
}
}
if (flag == ){
temp++;
}
else{
flag = ;
}
} printf("%d\n",temp);
return ;
} int main(){
int n;
scanf("%d",&n);
countPrimes(n); return ;
}

提交答案:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALsAAAAiCAIAAACImz+2AAACH0lEQVR4nO2Zy42DMBRFpzZEKSidQCGwogHWSBRAA9BAOmA3Cxts/MPXmGRGukes4u/Dx89G+dkIQfj59gTIP4PGEAwaQzBoDMGgMQSDxhAMGkMwaAzBoDEEg8YQDBpDMGgMwUCNWeZX0ZfW09RTWfRlvead3dIOZdG/2rdZMILDmfXXpphGXzXjqeYldf53GSd3+E+0iiWXMY7XnwGvMbdYm6IvaUwiqaeSuZbaJh7rviz6pt3dqtdte3eVWAB9nY4fPesXl2Oihjvqn403Rfe/azETmaVkJ3vnep+ntBcI0F+keptGcz5prfLymDHeR75TsdH1Z+isvRxvzMVwt43RM5N0VLS1k+51gP4iVwrf55PWKjvPGVOvRzW5IUSdal7s5iJm614CGBMc7nyPwU4lI8ZXpSUbI/doPQcCvCzae5MZRdRMa/UAz51Ko/m7DLKaF19isG4MwKkUHC6PMWraqu1Yu7NjIMDLIpX5tJyX1uoBPmKMrPYpY+zhIGP87/q4RihFzNXaTr/TGMVNYyI/gv6UMWIy8oq9y+06EYZuCQV4WRR1KsW1eoAvGeO6xzmC3O8l1nUvgzGu3OD8uhZu+XQHbr6BO6y/t7utsvM1YzbjFYR2T2ZjVLfmoF5j5MbdDRMzd337nA7WQID+IrX8Q9caOS+tVV74LwHBoDEEg8YQDBpDMGgMwaAxBIPGEAwaQzBoDMGgMQSDxhCMXz8bWKb+xM4ZAAAAAElFTkSuQmCC" alt="" />

失败!

自己用49969这个值测试了一下,结果用时24秒

好吧,虽然是比之前40秒少了不少,但是时间还是太长了啊

第三次尝试解决

通过第二次尝试解决联想一下,上面的情况是把n除以2之后,把后面一大部分去去掉(比如40/2 = 20 ,然后去掉20~40之间的数,不检查),那么除以3,除以4呢,是否可以以此类推?

通过运算验证,这个算法思路是正确的

那么从2开始,如果不能被整除,然后去掉后面1-1/2部分(对于40来说,就是20~40部分,大约20个数)

再从3开始,然后去掉后面1-1/3部分(对40来说,就是13~40部分,结合上面一步,本次省略了 13~20,也就是大约7个数)

在从4开始……

如此一来,这样就可以减少了大量的不必要的测试项了!

做法:引入变量t = 1,每检查一次,t = t+1,然后 i = j / i

int t;
for (int i = ; i < n; i++){
t = ;
for (int j = ; j < i/t; j++){
if (i%j == ){
flag = ;
break;
}
t++;
}
if (flag == ){
temp++;
}
else{
flag = ;
}
}

完整代码:

#include <stdio.h>

int countPrimes(int n) {
if (n == || n == || n == ){
printf("0\n");
return ;
}
int temp = ;
int flag = ;
int t;
for (int i = ; i < n; i++){
t = ;
for (int j = ; j < i/t; j++){
if (i%j == ){
flag = ;
break;
}
t++;
}
if (flag == ){
temp++;
}
else{
flag = ;
}
} printf("%d\n",temp);
return ;
} int main(){
int n;
scanf("%d",&n);
countPrimes(n); return ;
}

测试了 49969这个值,结果很快就出来,不到1秒钟!

这下可以通过了吧,应该可以AC了

提交一下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALsAAAAiCAIAAACImz+2AAACH0lEQVR4nO2Zy42DMBRFpzZEKSidQCGwogHWSBRAA9BAOmA3Cxts/MPXmGRGukes4u/Dx89G+dkIQfj59gTIP4PGEAwaQzBoDMGgMQSDxhAMGkMwaAzBoDEEg8YQDBpDMGgMwUCNWeZX0ZfW09RTWfRlvead3dIOZdG/2rdZMILDmfXXpphGXzXjqeYldf53GSd3+E+0iiWXMY7XnwGvMbdYm6IvaUwiqaeSuZbaJh7rviz6pt3dqtdte3eVWAB9nY4fPesXl2Oihjvqn403Rfe/azETmaVkJ3vnep+ntBcI0F+keptGcz5prfLymDHeR75TsdH1Z+isvRxvzMVwt43RM5N0VLS1k+51gP4iVwrf55PWKjvPGVOvRzW5IUSdal7s5iJm614CGBMc7nyPwU4lI8ZXpSUbI/doPQcCvCzae5MZRdRMa/UAz51Ko/m7DLKaF19isG4MwKkUHC6PMWraqu1Yu7NjIMDLIpX5tJyX1uoBPmKMrPYpY+zhIGP87/q4RihFzNXaTr/TGMVNYyI/gv6UMWIy8oq9y+06EYZuCQV4WRR1KsW1eoAvGeO6xzmC3O8l1nUvgzGu3OD8uhZu+XQHbr6BO6y/t7utsvM1YzbjFYR2T2ZjVLfmoF5j5MbdDRMzd337nA7WQID+IrX8Q9caOS+tVV74LwHBoDEEg8YQDBpDMGgMwaAxBIPGEAwaQzBoDMGgMQSDxhCMXz8bWKb+xM4ZAAAAAElFTkSuQmCC" alt="" />

失败!

怎么回事!这也不行!这leetcode的要求这么高?难道它的时间要求设置为0.1秒这样的级别?!

这次提示是 1500000 这个值测试不得通过,时间还是太长了。居然拿这么大的值来测试!

本机测试了一下,用时大约2秒(光标闪了两下的时间)就出来了,不过对于严格的leetcode来说,时间还是太长了。

第四次尝试解决

我意识到这个问题不能用上面的算法来解决,因为这个算法对于那个 质数的判断定义 来说,感觉已经是最优算法了。只能重新设计算法,于是把代码全部删了,重新再来!

拿起草稿纸,好好思考一下如何解决,思考存在什么更好的方法以及规律,思考发现了一些规律如下:

比如18,它能被2,3,6,9 整除,而6又能被2整除,所以在检测到被6整除之前,肯定已经检查到可以被2整除了,已经break了,所以我发现,凡是2的倍数,只要用2来判断就好了,也就是说2代表了所有的双数。同样的,3也是,有了3,就不用9来检查了,能被9整除的肯定能被3整除。

于是我们来观察下面一组数字:

2,3,4,5,6,7,8,9,10,11

2符合要求

3符合要求

4由于是2的倍数,所以不符合要求

5符合要求

6由于是2的倍数,所以不符合要求

7符合要求

8由于是2的倍数,所以不符合要求

9由于是3的倍数,所以不符合要求

10由于是2的倍数,所以不符合要求

11符合要求

我们可以发现,符合要求的都是质数(关于用数学来证明这个结论,不是讨论的重点)

也就是说,我们仅仅需要检测一个数是否能被比它小的质数整除,如果可以,说明它不是质数,如果都不可以,说明他是质数

转换成编程思想:

  用一个数组arr来存放这些质数,先给定arr[0] 为2,初始化,再用2来判断下一个值是否是质数,如果是,那么arr[1] == 该质数,再检查下一个数,检查是否可以被arr[0] 和 arr[1] 整除,以此类推

分析:下面代码定义的数组长度为200,不用太大,因为第200个质数的值是1223,那么它的平方是:1495729,对于n = 1500000来说,绰绰有余了

当然,为了保守起见,这个值可以设置大一点。

为了提高运算速度,这里限制了作为基数的质数个数为200个,超过200个部分,不进行运算,因为也没必要。

if (flag == false){
if (k < ){
k++;
arr[k] = i;
}
temp++;
}

代码实现:

#include <stdio.h>

int countPrimes(int n) {
if (n == || n == || n == ){
return ;
}
if (n == ){
return ;
} int temp = ;
bool flag = false;
int arr[] = { '\0' };
int k = ;
arr[] = ; for (int i = ; i < n; i++){
for (int j = ; j <= k; j++){
if (i%arr[j] == ){
flag = true;
break;
}
} if (flag == false){
if (k < ){
k++;
arr[k] = i;
}
temp++;
}
else{
flag = false;
}
} return temp+;
} int main(){
int n;
scanf("%d",&n);
printf("%d\n",countPrimes(n)); return ;
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAALUAAABwCAIAAADXFKb5AAAEDElEQVR4nO2ZMY7bMBAA/acr8qV8Ia8IkibfUR0k1VW5Kn0e4RQHGDqSO1zJom3JMxAMZbVaUdohrfhOLy+f3Nyi7XT3Ebg98nZ6FYk5/ROJ0Q8h9EMI/RBCP4TQDyH0Qwj9EEI/hOj78fX7j0XbDQYtNyPlx+ufv9H2+cu3+ad+HIyUHz9//U5u+nEwUn6c0ujHwdAPIRb4cT6fz+fz3Ibin/pxPLJ+XFR435l/6seBWexHsX4Ucf04GFetH7U3+nEwrnr/mOvyvqMfB8P/vwihH0L49xch/PutEPohhH4IoR9C6IcQ+iGEfgihH0LohxChH28ib2/kR/7PLnJY9EMI/RBCP4TI+DFN0/yUqSI6dLO4jKLrR92JqDfNXt4gLgMBP6KZmvFjfuLouAxkxPpRiDU6LgNZ7UfdoXn/Tq3vhUFxGcgKP+bMjyan++ZxGcgIP4pDo+MykCvfP6Bh0UTfPC4DAT+mistZzWARj/JHxGUU3fVDnhr9EEI/hNAPIfRDCP0QQj+EyPgR/dIAv0A0f5/gH0umj7+z+TvHQ9D1I+oQdK7Z10ykW1luDfgRzWCe2bwe1JlRhS1uTq5m2/XjEnT9OAgb+lGvGZn8emmJ4nIHNveDux4Nw7XkQRnxfto8qh+75I5+zCOZfbkD4Ef0ZQFfInUC50dFoLjclO76IU+NfgihH0Ks84NfC5pvJDyM6BWYz1pdmQ8tHczqwe8A9mPRE+Sji05Jvvw2yV+3e3fFq/SiOk/hx+njw7qmJRk/oGa3GjT7UrDYaV4rcqIrU3G5/L08NHk/5mSafUorlW92lAP53fZzwvxeuGD3pnbJovcP7nQdz885ToCrs3/Nypn8+fjrz+Y4uze1S8a9n0bPrn6OST9WwL4WQdarq1odqQvuD/CjfljJ+1/U7xV+LBpGs8fcvO4tLxptJuFxWff+wVweYreR3TRofPO6EKxLcT7s80Xru2ueuA8W+ZGcTNDO5j484iZ5jepDU8vI6LrgFo/zCGa8s9SP4vTanjqNK3TLZvIzxeuxdX2KPpv53fq7ZPP1oz4lOgTBrh8RUPzSPzixHm19qGtAVG2XbLh+RMHm3OIiySebSVsqa/LcKL8rLqjMNQfld0j6AZOvOcRkMDrEJi1Sthh/8iqZCxX5cN1uMrADPy4XSzYySuOud8s26zT3iyAoUhQvnmxXxMwM2axVd2H19wvMSJim8DSnmOLcrmc8zswlIBPyo+COFen6MQca3ww2p9cmj4/bdi+iMdx9YOtZ5Ic8HfohhH4IoR9C6IcQ+iGEfgihH0LohxD6IYR+CKEfQuiHEPohhH4IoR9C6IcQ+iGEfgihH0LohxD6IYR+CKEfQuiHEPohhH4IoR9C6IcA/wFAAIGDRfDnpAAAAABJRU5ErkJggg==" alt="" />

用时大约0.1秒,这下应该可以通过了。

提交一下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIgAAAAmCAIAAACQzkSbAAABqElEQVRoge2WsZGDMBBFKUzqBTehHHL3YIYmiKACKjCXuAuyC4SsFQiMb6zjH/ffEHhkWMF/aEX2RSDJjr4BEicbCSQUAwrFgEIxoFAMKBQDCsWAQjGgUAwoFAMKxYBCMaBQDCipxQy3q1ZGq7pLPFGcttZF/+5FXWG0ulb3FDe0m8RiHlVutDJambJNO1OEttbKUEyMe3NRRhd1uQjIraS5s7XxqZQ9RCl7ftn2ZXiVrOPX60qR4PyiP78Yl5pdN76bhamtpSnc2HdfHnkzrJSymUbEvF/kSFKK8T6cITkehl70r8Z9UrLalKmLuCvEagha2UaRcN5pVZ1YzNTH+vnvsS9FlILN8cVxuT3GuXI3kS0SiNkoYv/yC/rkrSzWH+zDz4NwbI9TzGfw32OLPSPWOvJmeDEeT+rHrWx5q/+jlck31yJi+sDm74Lb2rf9VSub/54iR5FIzPSo8pN0ehNdxxAx2aY07h6P7eHPTiUDfa7apap59N5N3lSnbmW/xHyPOQ0UAwrFgPLXxZwWigGFYkChGFAoBhSKAYViQKEYUCgGFIoB5Rtq6DrfbemqtwAAAABJRU5ErkJggg==" alt="" />

成功!

终于通过了!还记得当初认为它是水题吗?看来通过率低原来是这么来的……

 

引发的思考

从运行时间上来看本题的运算提升过程: 从 40秒 --> 24秒 -->1.5秒 --> 715ms --> 320ms --> 48ms --> 36ms --> 28ms

01.秒 与 40秒的差距是400倍,一个好的算法和解决方案,可以节省400倍的时间!

在我们平时做项目或者为了完成某些功能达到某个结果的时候,也许仅仅是为了达到而达到,结果正确就ok,但是很多时候,这种情况下可能仅仅满足自己的要求或者是小范围内符合要求,在很广的面上来看,很有可能完全不能用。

比如数据库设计,设计得好的,很快就可以查询到所要的数据。设计不好的,在短期内可能发现不了什么问题,但是到了数据庞大的时候,也许就没有实用性。有前辈跟我讲过,他有个项目是交水电费系统,测试的区域是一个小镇,查询速度不错。后来推广了,范围扩大到一个市,结果查询某一家的水电费情况,居然用了5分钟才查出来,明显是没有做到合理的设计。

那么,如何从一开始就做到尽量防止存在后顾之忧呢?我思考如下:

第一,要对问题的本质有非常明确的了解,不能只知其一不知其二。比如上面我的解题过程,仅仅知道质数的判断定义,并没有对里面存在的规律进行深入了解,造成了解题失败。

第二,在解决问题之前,尽可能的思考多种解法方案,记录下来,分析各种方案在时间效率,空间使用度的优劣。再选上符合要求的最好的方案去实施。切忌想到一个方法,它可以解决问题,但是很有可能不是最优方法,但是为了快速解决问题而拒绝思考,最后可能会照成不必要麻烦。(重写代码,代价更高),这个道理可能大家都懂,但是很少人真正有这样去做一件事情。

第三,学会站在巨人的肩膀上。本次解题,由于想要锻炼自己的思维和练习手感,所以要求自己一定要独立完成。但是在我们平时解决问题中,要学会借力。很多前人研究过的方法,他也许用了好长的时间才研究出来的从目前看最优的方法,那么我们学习他的方法,理解他的思维,用他的方法这样我们可以快速解决问题,同时也把知识化为自己的,这样效率最高,所以不要固执,学会使用google,让自己站在巨人的肩膀上前行,收获会是最大的。


2015-05-19 优化算法

=====================

有博友提出了用筛法解此题是最优的,于是去百度查了一下筛法,大概算法如下:

给出要筛数值的范围解决一道leetcode算法题的曲折过程及引发的思考n,找出解决一道leetcode算法题的曲折过程及引发的思考n以内的素数p1,p2,p3,......,pk。先用2去筛,即把2留下,把2的倍数剔除掉;再用下一个素数,也就是3筛,把3留下,把3的倍数剔除掉;接下去用下一个素数5筛,把5留下,把5的倍数剔除掉;不断重复下去......

下面是我动手实现这个算法:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h> int countPrimes(int n) {
if (n == || n == || n == ){
return ;
} bool *judge = (bool*)malloc(sizeof(bool)*n);
memset(judge, true, sizeof(bool)*n); for (int i = ; i < sqrt((double)n); i++){
if (judge[i] == true){
for (int j = ; i*j < n; j++){
judge[i*j] = false;
}
}
}
int temp = ;
for (int i = ; i < n; i++){
if (judge[i] == true){
temp++;
}
} return temp;
} int main(){
int n;
scanf("%d", &n);
printf("%d\n",countPrimes(n));
return ;
}

LeetCode判断结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABIUAAACbCAIAAABzvbwCAAAVRUlEQVR4nO3dz2scV4IH8P2f6tTIwujiIQkZcAhraM8hhsDg5GAWG0dJJgoxOyRgHwKaCWFjHOGFsMFjsNebgYnARniGJQbHDGIChmTksAR80E2XvojeQ1V1v/rVVd1q+bWtz4cc0l2vXr3qKsP76r169S9DAAAAYviX2A0AAAA4ouQxAACAOOQxAACAOOQxcnt7e7GbAAAAR0rXPLa3fWf9vTMnTyz3kiRJkmRp5aX+2cvX7/84axf+9oUkSZLX1/8+4/5d/H399SRJkgu3n5N6uxy0ord84uSZ99bvbB8kTO1t33j/taWZTqh8IZ/FhQUAgBdDlzw2ePjZ6aXaLJAkycqb1x4OZjiwPDbbQZstnb6y9cuMladXQx4DAIBnqj2P7X6zupIGryt3Hvy8m2Wvvafb96+v/rqXJEmysvrN7tQH1m2fUprHKj/YYPfn7fvX339tKUmSpHdqfaZwfJA8Vl+VCwsAAO1a89juV2eTJEle/7Suoz/46yevJEmS/OaLx9MeWLd9Sg15LDN4cvPCyuy/qDwGAAARtOaxlrl5lc1N5RuntQ1+uHnpzMtLSZIkSyv98+UHocblntxdf+vV5V6SJL3lV99aT+fmlb+9+ySIjTVt+eW765fOvJo9Bbe0cvLMpRuVB69ay9Sf4972jUtnTq4sNVc92m/ww5+vvJUV7S2/eubSzR9ah7Um57HhcPjLzXPHkyQ5/uFm8fu97Tvr5/vZk3/VdqU/cCA4xODJ/16/fLZfeGrw/BeF37jDfMXBD39eP9/Pfpje8olKFQAAcES15rGda28kSZK88vutbutFTJfHTp67cKpXygO9E6u3fynv9/v1dPgnKHbqs4c1D7atfLg5aGrLL7fLlaTHez+Yb9mlTPUcBw+vvVmzW+npumy/109VzjnpvXGtZYixNY8Nh5sfHk+S5Fef/HX81S+3V09UDlZoV2Mea35scOVC9QI15bHBw/Xq2SbJ0un/MIAGAMCR1/35sWTptbOXr26OHyGrN10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3kygJ75TMMGlA5+VJbD8Mi5rHixQlP/1nlsSkycO0/hGAGcEO6D8+wcMcVbtPw3131LgQAILbC9MJnnsfqeoWz5bFO8xVbeq11z48Nq7XOJY916CHX9NPlsXbl+6UYyZ5FHmsf0q00uHMe6/CXhto81rQHAADPUk1HMYxP085XPFAea+y1TjtfcYr1POaYx2rnNXYa0ag/40qjC3uHxzRfcaL658cKP1fNg4BzzWNpzYWvu16D4n1XTfQz3dlNyR4AgGet2FEsdE8n9VQLHb6a4a2Z8lhjrzVsSac/5pe7rdXi88xjdeNe1U3BUFZNyS495JqdCxkwr9B6HgUN6yuWLn96KbZGZZvyWJh+u+exYfEijf/BbU1Y7z7YrfTniElXcpb5iiYrAgDEVOiyV5b0qOupBm9mGpWfIo+1j+5UmrIzPl7revelemrjx1zzWDF2FTftlH+oxl+7Qw85uB47DQMna9a7BwAAiORZzEmcRB4DAACOjgnzJSOQxwAAgKOk+2r5h08eAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiEMeAwAAiGP2PAYAAMABzZLHAAAAOCTyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBzyGAAAQBz/D+3iMN3ttOBbAAAAAElFTkSuQmCC" alt="" />

<0.1秒 效率确实高了不少

说明

本题还可以再优化, 使用筛法, 本程序存在 重复赋值的情况, 如2的倍数有6, 3也有6, 他们会重复把下标为6的数组赋值为flase

解法有时间再思考


2015-05-19 再优化算法

=========================

int countPrimes(int n) {

    if (n ==  || n ==  || n == ){
return ;
} bool *judge = (bool*)malloc(sizeof(bool)*n);
memset(judge, true, sizeof(bool)*n); int temp = ;
for (int i = ; i < sqrt((double)n); i++){
if (judge[i] == true){
int t; for (int j = i; j * i < n; j++){
t = j * i;
if (judge[t] != false){
temp++;
judge[t] = false;
}
}
}
} return (n-temp);
}

LeetCode 判断结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABIwAAACjCAIAAABngRIIAAAVg0lEQVR4nO3d34sUV6IH8Ps/1VMzDjIvLknIghKu0O5DhMBi8iAXRSfJZkLkLgnogzC7Em7EDF6QK66g15uFzYAyuMslgpFlWEGIO4aL4MO8zUu/DH0fqqr71K+uqp52uiZ+PuQh3XXq1KmuEs53zqlT/zIEAACgM/5l3g0AAABgTEgDAADoECENAACgQ4Q0AACADhHSKLOzszPvJgAAwJtpmpC2s3lv9ZOTR48s9qIoiqJoYemt/qmL1x/+NG2//u7ZKIqi91b/PuX+Tfx99b0oiqKzdw9IvU0OWtBbPHL05Cer9zb3krB2Nm99emxhqhPKX8j9uLAAAPAL0zakDR5fObFQGhCiKFr64NrjwRSNENKmO2i1hROXNl5OWXl8NYQ0AACYj3Yhbfu75aU4jV269+jn7SSQ7bzafHh9+de9KIqipeXvtls3Ql++pTikFX6wwfbPmw+vf3psIYqiqHd8darEvJeQVl6VCwsAAC20CmnbN05FURS9d7ms9z/461fvRFEU/eabZ20boS/fUkVISwxe3D67NP0vKqQBAMA8tQppNVP7CpurylfOihs8vX3h5NsLURRFC0v9M/mHq8blXtxf/fDdxV4URb3Fdz9cjaf25b+9/yLIkiVtefnD9Qsn302erFtYOnrywq3Cw1y1ZcrPcWfz1oWTR5cWqqse7Td4+udLHyZFe4vvnrxw+2ntANjkkDYcDl/ePn04iqLDn69nv9/ZvLd6pp88TVhsV/wDB4JDDF787/WLp/qZJxHPfJP5jRtMdxw8/fPqmX7yw/QWjxSqAACAN12rkLZ17f0oiqJ3fr/RbGGKdiHt6Omzx3u5kNA7snz3ZX6/36/GA0VBseNXHpc8LLf0+fqgqi0v7+YriY/3aTBds0mZ4jkOHl/7oGS33BN7yX7vHS+cc9R7/1rNYGRtSBsO1z8/HEXRr7766/ir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m/r8HbfrywPAEAjaSct6MtVPpizNdor7YzWdUprQ1pVrzU/Eax2xKF5SGvUay1+nztG45BWPnzXtocclgx66dlLVXIGox/vNfe/D0xIy16x8DfZr5DWKi2XDBCP/oaRj+j1t3vm3g3/MRZvTQAA5i0zO3HfQ1pZr3C6kNZoumNNr7XsmbRhsdaZhLQGPeSSFCukTSl/E2Vz2n6EtPoR4ZImR2FLMtMUy56jzJ1efUir2gMAgP1U0lEMM1Xb6Y57CmmVvda20x1bLBwyw5BWOi1y8nTHSqWNzuwdHtN0x/bKn0nL/IYlTxzONKTFNWe+brn8R+Vzh1Pd7lV/AwAAYL9lO4qZ7umknmqmw1cyEDZVSKvstYYtafQX/nyvtVh8liGtbISsuKnq+aHKuZ4VPe2SMhYOaa1idcfcPRFfn41R2aqQFubk5iFtmL1ymaVAKuJaUD5bIrjMVX/naDvd0VxHAIB5yvTjC2uHlPVUg5dFjcq3CGn140CFpmyNj1e7BH+untJMMtOQls1i2U1b+R+q8tdu0EMOrsdW2Sy3+Iv8BM+q3/Q1ORAhDQAAYL/sx5TGSYQ0AADgDTdhuuUcCGkAAMAbr/kC/q+fkAYAANAhQhoAAECHCGkAAAAdIqQBAAB0iJAGAADQIUIaAABAhwhpAAAAHSKkAQAAdIiQBgAA0CFCGgAAQIcIaQAAAB0ipAEAAHSIkAYAANAhQhoAAECHCGkAAAAdIqQBAAB0iJAGAADQIUIaAABAhwhpAAAAHSKkAQAAdIiQBgAA0CFCGgAAQIfMOKQBAACwRzMLaQAAALxuQhoAAECHCGkAAAAdIqQBAAB0iJAGAADQIUIaAABAhwhpAAAAHSKkAQAAdIiQBgAA0CFCGgAAQIcIaQAAAB0ipAEAAHSIkAYAANAhQhoAAECHCGkAAAAdIqQBAAB0iJAGAADQIUIaAABAh/w/1AifdEcKDe4AAAAASUVORK5CYII=" alt="" />


2015-05-19 最后一次优化

==========================

解题代码:

int countPrimes(int n) {

    if (n ==  || n ==  || n == ){
return ;
} bool *judge = (bool*)malloc(sizeof(bool)*n);
memset(judge, true, sizeof(bool)*n);
int t; int temp = ;
for (int i = ; i < sqrt((double)n); i++){
int j = i;
t = i*j; if (judge[i]){
for (; t < n; j++){
if (judge[t]){
temp++;
judge[t] = false;
}
t = j * i;
}
}
} return (n-temp);
}

LeetCode判断结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABJoAAACwCAIAAADIc5OSAAAV5UlEQVR4nO3dz2scV4IH8P2f6tTIwujiIQkZsAlraM8hhsDg5GAWG1tJJgoxOyRgHwKaCWFjHOGFsMFjsNebgYnARniGJQbHDGIMhnjksAR80E2XvojeQ1V1v/rVVd0qq13tzwcfrKpXr151teB99V69+pchAAAAHfQv824AAAAAsxDnAAAAOkmcAwAA6CRxDgAAoJPEOQAAgE4S5wAAADpJnAMAAOgkcQ4AAKCTxDkAAIBOEucAAAA6SZwDAADoJHEOAACgk8Q5AACAThLnAAAAOkmcAwAA6CRxDgAAoJPEOQAAgE4S5wAAADpJnAMAAOgkcY4p7e3tzbsJAADAsPU4t7d9Z/2D08ePLfeiKIqiaGnltf6Zy9fv/zRrArh9Poqi6K31v7fazKy/r78VRVF0/nZH6m1y0oLe8rHjpz9Yv7N9kCy2t33jwxNLM11Q/kYexo0FAIDF1mKcGzz84tRSaZSIomjlnWsPBzNUKs7NdtJqS6eubP0yY+Xx3RDnAADgpdBanNv9bnUlzm1X7jz4eTeJbnvPt+9fX/11L4qiaGX1u92pq9Xrn1Ic5wof2GD35+371z88sRRFUdQ7uT5Ttj5InCuvyo0FAIDZtRXndr85E0VR9NbnZTlh8NfP3oiiKPrNV0+mrVevf0oVcS4xeHbz/Mrsn6g4BwAAL5G24lzNxMLC7qrylXPyBo9vXjr9+lIURdHSSv9c/iGwcblnd9fffXO5F0VRb/nNd9fjiYX5rXefBamzpC2//HD90uk3kycAl1aOn750o/DQWW2Z8mvc275x6fTxlaXqqkfHDR7/+cq7SdHe8punL918XDuoNjnODYfDX26ePRpF0dGPN7Pb97bvrJ/rJ089Ft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EOAACgk8Q5AACAThLnAAAAOkmcAwAA6CRxDgAAoJPEOQAAgE4S5wAAADpJnAMAAOgkcQ4AAKCTxDkAAIBO+n8T1QFOabAlkwAAAABJRU5ErkJggg==" alt="" />

最后一次, 28ms, 我想可以收工了, 我知道里面还有可以再次优化的, 在运算时间上可以再快的, 可以和我讨论, 有好的算法欢迎评论留言