507. Perfect Number
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)
所有得因子之和等于该数字的数被成为完美数。给定要给数字,判断其是否为完美数
乍看题目比较简单,就直接写呗
public static boolean checkPerfectNumber(int num) {
if(num == 1) return false;
int sum = 0;
for(int i = 1; i < num; i++)
if(num % i == 0)
sum += i;
return sum == num;
}
可是提交了一下,发现超时,这时候就需要做一定的优化了。就像以前求素数的方法,对输入的数求平方根
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;
sum += num/i;
}
return sum == num;
}
507. Perfect Number的更多相关文章
- 【leetcode】507. Perfect Number
problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...
- 507. Perfect Number 因数求和
[抄题]: We define the Perfect Number is a positive integer that is equal to the sum of all its positiv ...
- [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 ...
- 507 Perfect Number 完美数
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...
- LeetCode算法题-Perfect Number(Java实现)
这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义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 ...
- [Swift]LeetCode507. 完美数 | 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
原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...
随机推荐
- Redis 持久化之RDB和AOP
Redis 持久化之RDB和AOP Redis 有两种持久化方案,RDB (Redis DataBase)和 AOP (Append Only File).如果你先快速了解和使用RDB和AOP,可以直 ...
- Spring框架——IOC依赖注入
本来想把IOC和AOP一起介绍的,但是AOP内容太多了,所以就分开了,最后的结果就是这一篇只剩下一点点了.这不是第一次写关于IOC的文章了,之前写过Java反射,Java注解,也写过通过XML解析实现 ...
- C#检测获取移动硬盘盘符
最近做一个小工具, C# 对 移动硬盘的检测, var arr = DriveInfo.GetDrives(); 得出的所有磁盘,发现对于移动硬盘,DriveType 不是 Removable 类型 ...
- 一个部署了tomcat服务的linux服务器,运行一段时间后出现内存和空间不足的问题
—— 前段时间项目上的事比较忙,期间笔记都是临时存在本地txt,这些天有点时间了,整理出来,以便日后查看: linux 查看内存使用情况:free -m 释放缓存: /proc/sys/vm/drop ...
- unity中Ray、RaycastHit 、Raycast(小白之路)
1.Ray Ray(Vector3 origin, Vector3 direction) Ray:在程序中可以理解为射线,就是以某个位置(origin)朝某个方向(direction)的一条射线,是一 ...
- TeamTalk安装测试
TeamTalk介绍 项目框架 TeamTalk是蘑菇街的开源项目,github维护的最后时间是2015但是仍然是一款值得学习的好项目,麻雀虽小五脏俱全,本项目涉及到多个平台.多种语言,简单关系如下图 ...
- codeup模拟赛 进击的二叉查找数
问题 B: 进击的二叉查找树 时间限制: 1 Sec 内存限制: 64 MB 提交: 1017 解决: 379 提交状态 题目描述 给定1~N的两个排列,使用这两个排列分别构建两棵二叉查找树(也就是通 ...
- PHP根据传入参数合并多个JS和CSS文件的简单实现
HTML(使用方法): 复制代码代码如下: <link rel="stylesheet" type="text/css" href="cssmi ...
- angularjs 利用$http 请求出现 400 Bad Request
1. 出现400错误-代表错误的请求,说明我们的参数有问题 说明此时传入的参数存在问题,我们看下此时参数的格式是什么: 此时的参数是对象格式,查了一下,如果利用ajax格式传输数据的话,参数必须是js ...
- 掌握NIO,程序人生
就像新IO为java带来的革新那样,让我们也开启一段新的程序人生. 关键字:NIO,BIO,伪IO,AIO,多路复用选择器,通道,缓冲区,jdk研究,回调函数,高并发 java.nio 概述 历史背景 ...