[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为要新开一个动态数组,结果直接加到sum上就能省空间

以为要全都处理一遍,结果匹配地加上另一半就能省空间

[一句话思路]:

一对数匹配的情况,处理一半,另一半用公式就行了

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. sqrt前加Math

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

[复杂度]:Time complexity: O(n) Space complexity: O(1)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

class Solution {
public boolean checkPerfectNumber(int num) {
//cc
if (num == 1) return false; //ini sum = 1;
int sum = 1; //for loop
for (int i = 2; i <= (int)Math.sqrt(num); i++) {
if (num % i == 0) {
sum += i;
if (i * i != num) sum += num / i;
}
} return sum == num;
}
}

507. Perfect Number 因数求和的更多相关文章

  1. 【leetcode】507. Perfect Number

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

  2. 507. Perfect Number

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

  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. 507 Perfect Number 完美数

    对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”.给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False示例:输入: 28输出: True解释: ...

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

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

  7. [LeetCode] Perfect Number 完美数字

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

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

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

  9. LeetCode Perfect Number

    原题链接在这里:https://leetcode.com/problems/perfect-number/#/description 题目: We define the Perfect Number ...

随机推荐

  1. LAMP环境运行中为PHP添加CURL模块

    这里是自己遇到的问题记录并总结 1.—— : LAMP环境所需源码包在 /websrc 下 [保存了WEB环境所需的各种tar.gz 源码包]命名为资源目录 2.—— : LAMP环境源码包统一解压到 ...

  2. 在linux中使用shell来分析统计日志中的信息

    在运维工作中,要经常分析后台系统的日志,通过抓取日志中的关键字信息,对抓取结果进行统计,从而为监控结果提供基础数据.下面的shell演示了如何从大量的日志中取得想要的统计结果.其中展示了各种有趣的命令 ...

  3. python库之threading

    This module constructs higher-level threading interfaces on top of the lower level python库之_threadmo ...

  4. 拦截器springmvc防止表单重复提交【3】自己实际项目

    1:[定义注解] package com.jspxcms.ext.interceptor; import java.lang.annotation.ElementType; import java.l ...

  5. BZOJ4260,LOJ10051 Nikitosh 和异或

    题意 给定一个含 \(N\) 个元素的数组 \(A\),下标从 \(1\) 开始.请找出下面式子的最大值:\((A[l_1]\bigoplus A[l_1+1]\bigoplus -\bigoplus ...

  6. 使用.NET Remoting开发分布式应用——配置文件篇

    我们已经知道可以通过编码的方式配置服务器通道和远程客户机,除此之外,还可以使用配置文件对服务器通道和远程客户机进行配置.使用远程客户机和服务器对象的配置文件的优点在于,用户无需修改任何一行代码,也无需 ...

  7. 笔记:开源协议 Apache 2 和 GPL 兼容

    笔记:开源协议 Apache 2 和 GPL 兼容 Apache 2 和 GPL v3 兼容. GPL 分了很多版本,LGPL 为最宽松的 GPL,而 AGPL 为最严格的 GPL 协议. Linux ...

  8. GitFlow在客户端Sourcetree的使用

    安装 Sourcetree中直接集成了gitflow工具,可以在界面上找到  初始化 首次按下Git Flow按钮后,会弹出如图窗口  初始化会规定几个特殊的分支名称 生产环境分支:master 开发 ...

  9. 【转】JMeter脚本的参数化

    JMeter脚本的参数化 当你利用Badboy将你的测试脚本录制完毕后,接下来就是脚本的调试工作了.在我看来,调试应该包括有以下几个方面:1.根据测试场景对脚本进行必要的修改:2.脚本参数化:3.添加 ...

  10. java成神之——文件IO

    文件I/O Path Files File类 File和Path的区别和联系 FileFilter FileOutputStream FileInputStream 利用FileOutputStrea ...