Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Index/数学.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
| [470. 用 Rand7() 实现 Rand10()](https://leetcode-cn.com/problems/implement-rand10-using-rand7/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/implement-rand10-using-rand7/solution/gong-shui-san-xie-k-jin-zhi-zhu-wei-shen-zmd4/) | 中等 | 🤩🤩🤩🤩 |
| [477. 汉明距离总和](https://leetcode-cn.com/problems/total-hamming-distance/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/total-hamming-distance/solution/gong-shui-san-xie-ying-yong-cheng-fa-yua-g21t/) | 简单 | 🤩🤩🤩 |
| [483. 最小好进制](https://leetcode-cn.com/problems/smallest-good-base/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/smallest-good-base/solution/gong-shui-san-xie-xiang-jie-ru-he-fen-xi-r94g/) | 困难 | 🤩🤩🤩🤩 |
| [507. 完美数](https://leetcode-cn.com/problems/perfect-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/) | 简单 | 🤩🤩🤩 |
| [523. 连续的子数组和](https://leetcode-cn.com/problems/continuous-subarray-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/continuous-subarray-sum/solution/gong-shui-san-xie-tuo-zhan-wei-qiu-fang-1juse/) | 中等 | 🤩🤩🤩🤩 |
| [552. 学生出勤记录 II](https://leetcode-cn.com/problems/student-attendance-record-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-ii/solution/gong-shui-san-xie-yi-ti-san-jie-ji-yi-hu-fdfx/) | 困难 | 🤩🤩🤩🤩 |
| [633. 平方数之和](https://leetcode-cn.com/problems/sum-of-square-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-square-numbers/solution/gong-shui-san-xie-yi-ti-san-jie-mei-ju-s-7qi5/) | 简单 | 🤩🤩 |
Expand Down
1 change: 1 addition & 0 deletions Index/模拟.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
| [495. 提莫攻击](https://leetcode-cn.com/problems/teemo-attacking/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/teemo-attacking/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-gteh/) | 简单 | 🤩🤩🤩🤩🤩 |
| [500. 键盘行](https://leetcode-cn.com/problems/keyboard-row/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/keyboard-row/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-zx6b/) | 简单 | 🤩🤩🤩🤩 |
| [506. 相对名次](https://leetcode-cn.com/problems/relative-ranks/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/relative-ranks/solution/gong-shui-san-xie-jian-dan-pai-xu-mo-ni-cmuzj/) | 简单 | 🤩🤩🤩🤩 |
| [507. 完美数](https://leetcode-cn.com/problems/perfect-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/) | 简单 | 🤩🤩🤩 |
| [520. 检测大写字母](https://leetcode-cn.com/problems/detect-capital/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/detect-capital/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-rpor/) | 简单 | 🤩🤩🤩🤩 |
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
| [541. 反转字符串 II](https://leetcode-cn.com/problems/reverse-string-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-string-ii/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-p88f/) | 简单 | 🤩🤩🤩🤩🤩 |
Expand Down
88 changes: 88 additions & 0 deletions LeetCode/501-510/507. 完美数(简单).md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
### 题目描述

这是 LeetCode 上的 **[507. 完美数](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/)** ,难度为 **简单**。

Tag : 「模拟」、「数论」、「数学」



对于一个 正整数,如果它和除了它自身以外的所有 **正因子** 之和相等,我们称它为 「完美数」。

给定一个 整数 `n`, 如果是完美数,返回 `true`,否则返回 `false`。

示例 1:
```
输入:num = 28

输出:true

解释:28 = 1 + 2 + 4 + 7 + 14
1, 2, 4, 7, 和 14 是 28 的所有正因子。
```
示例 2:
```
输入:num = 6

输出:true
```
示例 3:
```
输入:num = 496

输出:true
```
示例 4:
```
输入:num = 8128

输出:true
```
示例 5:
```
输入:num = 2

输出:false
```

提示:
* $1 <= num <= 10^8$

---

### 数学

我们知道正因数总是成对的出现,因此我们可以仅枚举每对正因数的较小数,即从 $[1, \sqrt{num}]$ 范围内进行枚举(其中 $nums > 1$)。

同时为避免使用 `sqrt` 库函数和溢出,使用 $i <= \frac{num}{i}$ 作为上界判断。

代码:
```Java
class Solution {
public boolean checkPerfectNumber(int num) {
if (num == 1) return false;
int ans = 1;
for (int i = 2; i <= num / i; i++) {
if (num % i == 0) {
ans += i;
if (i * i != num) ans += num / i;
}
}
return ans == num;
}
}
```
* 时间复杂度:$O(\sqrt{num})$
* 空间复杂度:$O(1)$

---

### 最后

这是我们「刷穿 LeetCode」系列文章的第 `No.507` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。

为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。

在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。