LeetCode Perfect Number
原题链接在这里:https://leetcode.com/problems/perfect-number/#/description
题目:
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)
题解:
把每个divisor相加看是否等于num.
Time Complexity: O(Math.sqrt(num)). Space: O(1).
AC Java:
public class Solution {
public boolean checkPerfectNumber(int num) {
if(num == 1){
return false;
} int sum = 1;
for(int i = 2; i<Math.sqrt(num); i++){
if(num%i == 0){
sum += i+num/i;
}
}
return sum == num;
}
}
LeetCode Perfect Number的更多相关文章
- [LeetCode] 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 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】507. Perfect Number
problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...
- C#LeetCode刷题之#507-完美数(Perfect Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...
- [Swift]LeetCode507. 完美数 | Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- 507. Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
随机推荐
- try catch 事务不会滚
在spring机制中,在配置事务后,如果采用try catch 捕获异常后,因为异常已经被捕获,所以事务不会滚,从而产生许多脏数据.解决办法: 1.在catch中抛出异常,(throw new Run ...
- python之路 正则表达式,模块导入的方法,hashlib加密
一.正则表达式re python中re模块提供了正则表达式相关操作 字符: . 匹配除换行符以外的任意字符 \w 匹配字母或数字或下划线或汉字 \s 匹配任意的空白符 \d 匹配数字 \b 匹配单词的 ...
- php异常处理类
<?php header('content-type:text/html;charset=UTF-8'); // 创建email异常处理类 class emailException extend ...
- 011_Eclipse中使用HDFSFileSystemAPI事例介绍
需求 1.文件操作 1)上传本地文件到HDFS 2)读取文件 3)在hadoopfs中新建文件,并写入 4)重命名文件 5)删除hadoopfs上的文件 2.目录操作 1)读取某个目录下的所有文件 2 ...
- four application:geocoder widget
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- js判断一个数组是否包含一个指定的值
今天看了一下 有好几种方法 总结一下 1:array.indexOf 此方法判断数组中是否存在某个值,如果存在返回数组元素的下标,否则返回-1 let arr = ['something', ...
- 以太坊钱包Geth使用命令
一.启动以太坊钱包Geth 打开一个控制台,执行同步区块命令 #同步测试链geth --fast --cache=512 --rpc --rpcapi personal,db,eth,net,web3 ...
- sql server update时,是行锁还是表锁
https://bbs.csdn.net/topics/120000749 http://www.cnblogs.com/s021368/articles/2148659.html 问题: udpat ...
- MVC。
mvc 开启客户端 和 远程验证 <appSettings> <add key="ClientValidationEnabled" value="tru ...
- web 程序中的编码
比如字符串 <script type="text/javascript">alert('跨站攻击鸟')</script> 1.html encode ...