Skip to content

Commit 2b88a76

Browse files
Merge pull request #292 from SharingSource/ac_oier
✨feat: Add 507
2 parents b9c1ecc + 832accc commit 2b88a76

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Index/数学.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
| [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/) | 中等 | 🤩🤩🤩🤩 |
3030
| [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/) | 简单 | 🤩🤩🤩 |
3131
| [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/) | 困难 | 🤩🤩🤩🤩 |
32+
| [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/) | 简单 | 🤩🤩🤩 |
3233
| [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/) | 中等 | 🤩🤩🤩🤩 |
3334
| [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/) | 困难 | 🤩🤩🤩🤩 |
3435
| [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/) | 简单 | 🤩🤩 |

Index/模拟.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
5252
| [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/) | 简单 | 🤩🤩🤩🤩 |
5353
| [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/) | 简单 | 🤩🤩🤩🤩 |
54+
| [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/) | 简单 | 🤩🤩🤩 |
5455
| [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/) | 简单 | 🤩🤩🤩🤩 |
5556
| [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/) | 中等 | 🤩🤩🤩🤩 |
5657
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[507. 完美数](https://leetcode-cn.com/problems/perfect-number/solution/gong-shui-san-xie-jian-dan-mo-ni-tong-ji-e6jk/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」、「数论」、「数学」
6+
7+
8+
9+
对于一个 正整数,如果它和除了它自身以外的所有 **正因子** 之和相等,我们称它为 「完美数」。
10+
11+
给定一个 整数 `n`, 如果是完美数,返回 `true`,否则返回 `false`
12+
13+
示例 1:
14+
```
15+
输入:num = 28
16+
17+
输出:true
18+
19+
解释:28 = 1 + 2 + 4 + 7 + 14
20+
1, 2, 4, 7, 和 14 是 28 的所有正因子。
21+
```
22+
示例 2:
23+
```
24+
输入:num = 6
25+
26+
输出:true
27+
```
28+
示例 3:
29+
```
30+
输入:num = 496
31+
32+
输出:true
33+
```
34+
示例 4:
35+
```
36+
输入:num = 8128
37+
38+
输出:true
39+
```
40+
示例 5:
41+
```
42+
输入:num = 2
43+
44+
输出:false
45+
```
46+
47+
提示:
48+
* $1 <= num <= 10^8$
49+
50+
---
51+
52+
### 数学
53+
54+
我们知道正因数总是成对的出现,因此我们可以仅枚举每对正因数的较小数,即从 $[1, \sqrt{num}]$ 范围内进行枚举(其中 $nums > 1$)。
55+
56+
同时为避免使用 `sqrt` 库函数和溢出,使用 $i <= \frac{num}{i}$ 作为上界判断。
57+
58+
代码:
59+
```Java
60+
class Solution {
61+
public boolean checkPerfectNumber(int num) {
62+
if (num == 1) return false;
63+
int ans = 1;
64+
for (int i = 2; i <= num / i; i++) {
65+
if (num % i == 0) {
66+
ans += i;
67+
if (i * i != num) ans += num / i;
68+
}
69+
}
70+
return ans == num;
71+
}
72+
}
73+
```
74+
* 时间复杂度:$O(\sqrt{num})$
75+
* 空间复杂度:$O(1)$
76+
77+
---
78+
79+
### 最后
80+
81+
这是我们「刷穿 LeetCode」系列文章的第 `No.507` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
82+
83+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
84+
85+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
86+
87+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
88+

0 commit comments

Comments
 (0)