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 ...
随机推荐
- Centos下mongodb的安装与配置
安装MongoDB的方法有很多种,可以源代码安装,在Centos也可以用yum源安装的方法. 1.准备工作 运行yum命令查看MongoDB的包信息 yum info mongodb-org (提示没 ...
- android 开源收藏
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- windows下安装和redis主从配置(通过哨兵控制主从切换)
首先自己先得了解什么是redis,这里就不详做介绍什么是redis了,这篇文章主要讲的是怎么样配置 redis怎样配置主从关系和哨兵控制主从服务器的配置以及应用,就当是给自己记笔记吧! 1.下载red ...
- Animation-list,帧动画+属性动画,做出Flash般的效果
我们会用到PS,即使不会也不要怂,只需要几步傻瓜式操作即可. 属性动画可以看看我另一篇文章:属性动画详解 效果图 相信机智的各位,看完之后一定能发挥创意,做出更酷更炫的效果 图层获取 首先你需要找一张 ...
- OC中只有重写没有重载
一.类的继承 Objective-c中类的继承与C++类似,不同的是Objective-c不支持多重继承,一个类只能有一个父类,单继承使Objective-c的继承关系很简单,易于管理程序. 二.方法 ...
- TCP/IP 协议栈 ------ ICMP
I C M P经常被认为是 I P层的一个组成部分.它传递差错报文以及其他需要注意的信息.I C M P报文通常被I P层或更高层协议( T C P或U D P)使用.一些I C M P报文把差错报文 ...
- OpenStack搭建遇到的问题2(组件配置错误了,别重装全部,就把模块卸载就行了)
apt-get remove -y mysql-server python-mysqldb 在装OpenStack的时候,出错的可能就是就是一个模块,比如keysstone或者是glance出错了,我 ...
- Spring Cloud 之 Feign
新建Spring Boot工程,命名为feign 1.pom.xml添加依赖 <?xml version="1.0" encoding="UTF-8"?& ...
- Spring Cloud Ribbon 整合 Hystrix
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造 1.pom.xml 加入依赖 <dependency> <groupId>org.sp ...
- Qname
Qname的全称是qualified name. Qname由三部分组成: 1.Namespace prefix 2.A colon character(":") 3.A loca ...