[LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself.
Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not.
Example:
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)
这道题让我们判断给定数字是否为完美数字,并给来完美数字的定义,就是一个整数等于除其自身之外的所有的因子之和。那么由于不能包含自身,所以n必定大于1。其实这道题跟之前的判断质数的题蛮类似的,都是要找因子。由于1肯定是因子,可以提前加上,那么我们找其他因子的范围是[2, sqrt(n)]。我们遍历这之间所有的数字,如果可以被n整除,那么我们把i和num/i都加上,对于n如果是平方数的话,那么我们此时相同的因子加来两次,所以我们要判断一下,如果相等,就不再加 num/i。实际上,符合要求的完美数字很少,根本就没有完全平方数,我们根本就不用担心会加两次,当然,这都是从结果分析的,为了严格按照题目的要求,还是加上判断吧。还有就是我们也可以在遍历的过程中如果累积和sum大于n了,直接返回false,但是感觉加了也没咋提高运行时间,所以干脆就不加了。在循环结束后,我们首先判断num是否为1,因为题目中说了不能加包括本身的因子,然后我们再看sum是否和num相等,参见代码如下:
解法一:
class Solution {
public:
bool checkPerfectNumber(int num) {
int sum = ;
for (int i = ; i * i <= num; ++i) {
if (num % i == ) {
sum += i + (num / i == i ? : num / i);
}
}
return num != && sum == num;
}
};
下面这种方法叼的不行,在给定的n的范围内其实只有五个符合要求的完美数字,于是就有这种枚举的解法,那么套用一句诸葛孔明的名言就是,我从未见过如此厚颜无耻之解法。哈哈,开个玩笑。写这篇博客的时候,国足正和伊朗进行十二强赛,上半场0比0,希望国足下半场能进球,好运好运,不忘初心,方得始终~
解法二:
class Solution {
public:
bool checkPerfectNumber(int num) {
return num == || num == || num == || num == || num == ;
}
};
类似题目:
参考资料:
https://leetcode.com/problems/perfect-number/
https://leetcode.com/problems/perfect-number/discuss/98594/simple-java-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Perfect Number 完美数字的更多相关文章
- [LeetCode] 507. Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- LeetCode Perfect Number
原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- [LeetCode] Perfect Rectangle 完美矩形
Given N axis-aligned rectangles where N > 0, determine if they all together form an exact cover o ...
- LeetCode Happy Number 开心数字
题意: 给出一个整数n,判断其是否为幸运数. 规则是,将n按十进制逐位拆出来后,每个位各自进行取平方,再将这些平方数求和作为新的数字n.若最后n=1,就是幸运数. 思路: 计算例子:n=47,接着n= ...
- [Swift]LeetCode507. 完美数 | Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- LeetCode算法题-Perfect Number(Java实现)
这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- (转)关于 awk 的 pattern(模式)
本文转自chinaunix http://bbs.chinaunix.net/thread-4246512-1-1.html 作者reyleon 我们知道, awk程序由一系列 pattern 以 ...
- JavaWeb学习笔记三 Servlet
Servlet 是运行在服务端的Java小程序,是sun公司提供一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API,动态的向客户 ...
- Java虚拟机16:Metaspace
被废弃的持久代 想起之前面试的时候有面试官问起过我一个问题:Java 8为什么要废弃持久代即Metaspace的作用.由于当时使用的Java 7且研究重心不在JVM上,一下没有回答上来,今天突然想起这 ...
- 测试工作中经常用到的几个Linux命令(第一弹)
自己平时测试工作中经常要在Linux下搭建测试环境,有涉及到启动/终止服务器,修改tomcat配置文件,偶尔碰到端口被占用... 这时就不得不需要一些基本的Linux命令来处理遇到的这些问题(顺便迈向 ...
- Windows下编译SDL
Windows下编译SDL的理由我就不多说了,无论用VS来编译或调试SDL库都是很方便的.而且SDL源代码中也包含了VC工程,你所要做的只是解压VC工程,进行适当的配置,然后编译.调试. 编译SDL大 ...
- HDFS的7个设计特点
1.Block的放置:默认不配置.一个Block会有三份备份,一份放在NameNode指定的DataNode,另一份放在与指定DataNode非同一Rack上的DataNode,最后一份放在与指定Da ...
- python小练习之一
下面的练习本身不难,比如打印1到10,计算1+2+3+...+100 ,最后一个是计算 1-2+3-4...-100 用了类的方法实现 用了列表生成器 用"高级"一丢丢的写法来实现 ...
- c 语言的基本语法
1,c的令牌(Tokens) printf("Hello, World! \n"); 这五个令牌是: printf ( "Hello, World! \n" ) ...
- ( 转 ) CORS 有一次 OPTIONS 请求的原理
刚接触前端的时候,以为HTTP的Request Method只有GET与POST两种,后来才了解到,原来还有HEAD.PUT.DELETE.OPTIONS-- 目前的工作中,HEAD.PUT.DELE ...
- Python内置函数(56)——locals
英文文档: locals() Update and return a dictionary representing the current local symbol table. Free var ...