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 ...
随机推荐
- PAT 天梯赛 L2-024. 部落 【并查集】
题目链接 https://www.patest.cn/contests/gplt/L2-024 题意 给出 几个不同的圈子,然后 判断 有哪些人 是属于同一个部落的,或者理解为 ,有哪些人 是有关系的 ...
- 动手动脑:String.equals()的使用方法
public class StringEquals { /** * @param args the command line arguments */ public static void main( ...
- C# XMLHttpRequest对象—Ajax实例
Get: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- UI组件之UIImage
UIImageView:图像视图,用于在应用程序中显示图片 UIImage:是将图片文件转换为程序中的图片对象 UIImageView是UIImage的载体 方法一:用此方法创建图片对象,会将图片ca ...
- ODS
一般在带有ODS的系统体系结构中,ODS都设计为如下几个作用: 1.在业务系统和数据仓库之间形成一个隔离层 一般的数据仓库应用系统都具有非常复杂的数据来源,这些数据存放在不同的地理位置.不同的数据库. ...
- python 课堂笔记-for语句
for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...
- CSS3 3D折叠展开动画菜单
在线演示 本地下载
- 对vector,list的操作函数
向量只能接受同一类型的数据:list可以接受不同的数据. 1.添加元素 vector:> b=c(1,2,3) > b=c(b,"four") #直接在后面添加添加 & ...
- ceph安装各种报错
[ceph_deploy][ERROR ] RuntimeError: Failed to execute command: ceph-disk-activate –mark-init sysvini ...
- 导出android真机上应用的apk文件
1. 首先你的手机要开启调试模式 2. 终端输入命令行 (这个时候需要在手机端打开此应用.它的思路是抓取出当前窗口的包名.以下命令操作自己未亲自验证.) adb shell dumpsys windo ...