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 ...
随机推荐
- Python基础知识补充(重要)-作用域、特殊语法
Python作用域 python代码内部块如if语句内声明变量,在if代码段后在调用此变量并未报如“undefinded name"此类错误,例子如下: if 1 == 1: name = ...
- Android系统源代码的下载与编译
http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...
- usb2.0 协议分析
转:https://blog.csdn.net/u011594613/article/details/48291307 一.USB硬件介绍1.1.概述 一条USB传输线分别由地线.电源线.D+和D-四 ...
- ACM训练小结-2018年6月15日
今天题目情况如下:A题:给出若干条边的边长,问这些边按顺序能否组成一个凸多边形,并求出这个多边形的最小包含圆.答题情况:无思路.正解(某种):第一问很简单.对第二问,如果R大于可行的最小R,那么按照放 ...
- 安装配置Apollo-Prota web中心平台
首先要求2g以上内存哈,JDK1.8 搭建数据库 创建表 apollo-build-scripts-master 整个阿波罗环境包 使用一个命令启动整个阿波罗服务环境 创建两个数据库 分别为:apol ...
- cuffdiff problem--Performed 0 isoform-level transcription difference tests
如何解决cuffdiff运行结果中表达量为0的情况? cuffdiff -o cdiffout -b ref.fasta -u -p 15 --library-type fr-firststrand ...
- java深入探究15-SpringMVC
测试代码:链接:http://pan.baidu.com/s/1c1QGYIk 密码:q924 回顾spring+struts web.xml配置;struts核心过滤器;spring监听器-> ...
- UVA 11029 || Lightoj 1282 Leading and Trailing 数学
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...
- ANT+JMETER + Jenkins 集成1
新建任务注意添加invoke Ant,新建成功后运行就可以啦
- Linux 软链接操作项目
项目添加软链 ln -s 源文件 目标文件 指令 ln -s /home/wwwroot/default/nikon/panda/pc ./pc ln -s /home/wwwroot/default ...