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 ...
随机推荐
- 8.22 ps课堂练习
真是做得超烂!以前学的快忘光了!
- springboot-vue项目前台2
api_account.js import * as API from './' export default { //登录 login: params => { return API.POST ...
- console.log()方法中%s的作用
一.console.log("log信息"); 二.console.log("%s","first","second") ...
- codeforces Codeforces Round #318 div2 A. Bear and Elections 【优先队列】
A. Bear and Elections time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 修改redhat默认显示语言为中文
[delmore@localhost Desktop]$ su //切换到最高权限 Password: ...
- Linux下安装lrzsz上传下载工具
使用yum安装 为什么要使用yum安装? 答:安装十分方便,几乎不需要别的操作,只需要一个yum命令就可以完成所有的安装过程. yum -y install lrzsz 要有网络才行 输入命令:rz ...
- RedisDesktopManager连接不上redis的解决方法
RedisDesktopManager是一款连接redis数据库的客户端. 背景:我是在自己机器上装的redis,使用的是虚拟机,系统是linux 版本是centeros-6.7 在使用这个连接red ...
- 【bzoj1925】地精部落[SDOI2010](dp)
题目传送门:1925: [Sdoi2010]地精部落 这道题,,,首先可以一眼看出他是要我们求由1~n的排列组成,并且抖来抖去的序列的方案数.然后再看一眼数据范围,,,似乎是O(n^2)的dp?然后各 ...
- 解决可以Ping通ip地址,但Ping不通域名的思路
在正常的网络故障处理中,ping命令是大家经常用到的,出现ping通ip地址,但ping域名是出现超时情况,一般是由于TCP/IP协议中的“DNS设置”不正确,请检查其中的配置,或者更换其他可用的DN ...
- ssh学习(1)
雇员薪资管理系统(crud) ①先搞定spring ②引入spring包 ③编写applicationContext.xml文件(或者beans.xml),我们把该文件放在src目录下 ④测试一下sp ...