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 ...
随机推荐
- JSP笔记01——尝试
JSP ————> servlet 我的第1个Java Web应用程序——index.jsp 我的第2个Java Web应用程序——welcome-file 我的第3个Java Web应用程序— ...
- codeforces上某题
一道codeforces上的题目. 题目大意: 定义有k个不同的字符的字符串为好字符串.现在给出一个字符串,求解对该字符串的每个前缀Si至少是多少个好字符串的连接,若不能由好字符串连接而成则输出-1. ...
- list— 把数组中的值赋给一组变量
(PHP 4, PHP 5, PHP 7) list — 把数组中的值赋给一组变量 array list ( mixed $var1 [, mixed $... ] ) 像 array() 一样,这不 ...
- Virtual Container Hosts(VCHs) 介绍
In vSphere Integrated Containers, you deploy virtual container hosts (VCHs) that serve as Docker API ...
- java中如何将非整数保留到小数点后指定的位数
- oracle修改密码和设置密码有效期
一.修改密码1)修改密码 sql>alter user user01 identified by password; 2)修改密码并unlock sql>alter user user01 ...
- 【P1582】倒水(数论??暴力!!)
这个题我很无语,一开始看绿题,还是数论,应该不会特别简单,应该要动笔写上好一会,过了一会旁边 #祝神 说这原来是个蓝题,我顿时觉得十分迷茫... 结果看了这个题看了一会,仔细一想,woc,这题怕不是可 ...
- rpm卸载软件error preun
这两天,使用ipvsadm -ln总是显示空. 后来,使用strace ipvsadm -ln定位 看来,是ipvsadm模块有问题,卸载了再重新安装吧,结果出现这种问题. 从来没遇到这种问题: er ...
- ios点击事件失效
当使用委托给一个元素添加click事件时,如果事件是委托到 document 或 body 上,并且委托的元素是默认不可点击的(如 div, span 等),此时 click 事件会失效. 解决办法有 ...
- 初探MyBatis之HelloWorld(三)
三.用SQL映射语句用注解,dataSource用xml(不推荐). 综合上面两节(一个用xml,一个用annotation),发现一个好玩儿的,SQL映射用注解方式,然后还是得有两个xml配置文件. ...