原题链接在这里: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的更多相关文章

  1. [LeetCode] Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  2. LeetCode算法题-Perfect Number(Java实现)

    这是悦乐书的第249次更新,第262篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第116题(顺位题号是507).我们定义Perfect Number是一个正整数,它等于 ...

  3. [LeetCode] 507. Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  4. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【leetcode】507. Perfect Number

    problem 507. Perfect Number solution: /* class Solution { public: bool checkPerfectNumber(int num) { ...

  6. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  7. [Swift]LeetCode507. 完美数 | Perfect Number

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  8. 507. Perfect Number

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  9. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

随机推荐

  1. Zuul

    一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...

  2. 生成sql表结构

    DataConstruct.php <?php /** * Created by PhpStorm. * User: Administrator * Date: 2017/7/21 * Time ...

  3. LVS 介绍

    LVS 介绍 说明: LVS是Linux Virtual Server的简称 LVS是一个实现负载均衡的开源软件项目 LVS效率要高于Nginx LVS工作在ISO的第4层(传输层) LVS架构有三层 ...

  4. SOA 面向服务架构 阅读笔记(六)

    20 SOA质量 服务质量是成功的关键因素 20.1 了解SOA带来的无法预料的挑战 不同部门开发 每个开发的组件整合在一起,形成复合应用程序 整合业务流程,考虑质量问题 衡量SOA的质量. 事物质量 ...

  5. 20165101刘天野 2017-2018-2 《Java程序设计》第4周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 第五章:子类与继承 面向对象程序设计语言有三大特性:封装.继承和多态性.继承是面向对 ...

  6. Win32 API编程:CHAR TCHAR WCHAR的区别

    #ifdef   UNICODE               typedef   wchar_t   TCHAR;     #else               typedef   unsigned ...

  7. INSPIRED启示录 读书笔记 - 第13章 产品原则

    确定什么最重要 产品原则是对团队信仰和价值观的总结,用来指导产品团队作出正确的决策和取舍.它体现了产品团队的目标和愿景,是产品战略的重要组成部分.从形式上看,它是一系列明确的.体现团队特色的产品价值准 ...

  8. 斯坦福机器学习视频笔记 Week8 无监督学习:聚类与数据降维 Clusting & Dimensionality Reduction

    监督学习算法需要标记的样本(x,y),但是无监督学习算法只需要input(x). 您将了解聚类 - 用于市场分割,文本摘要,以及许多其他应用程序. Principal Components Analy ...

  9. maven deploy 代码

    Run As --> Run Configurations ---> Maven Build --->New _Configuration(双击Maven Build可生成) --& ...

  10. 【算法】fhqtreap初探

    NOIP回来就一直想着学平衡树...平衡树写久了调不出来真的会头脑发热.jpg 大概只写了几道题... fhqtreap是不需要旋转的平衡树,仅使用分裂合并,一样可以保持平衡树的性质,并且可以非常简单 ...