From 832accce287870d5a69b7bf5f30f7914bb4985f6 Mon Sep 17 00:00:00 2001 From: AC_Oier Date: Fri, 31 Dec 2021 17:32:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feat:=20Add=20507?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "Index/\346\225\260\345\255\246.md" | 1 + "Index/\346\250\241\346\213\237.md" | 1 + ...10\347\256\200\345\215\225\357\274\211.md" | 88 +++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 "LeetCode/501-510/507. \345\256\214\347\276\216\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index 8c684146..d3ed65fd 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -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/) | 简单 | 🤩🤩 | diff --git "a/Index/\346\250\241\346\213\237.md" "b/Index/\346\250\241\346\213\237.md" index 21db8a37..da9d924d 100644 --- "a/Index/\346\250\241\346\213\237.md" +++ "b/Index/\346\250\241\346\213\237.md" @@ -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/) | 简单 | 🤩🤩🤩🤩🤩 | diff --git "a/LeetCode/501-510/507. \345\256\214\347\276\216\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" "b/LeetCode/501-510/507. \345\256\214\347\276\216\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" new file mode 100644 index 00000000..b6c4360b --- /dev/null +++ "b/LeetCode/501-510/507. \345\256\214\347\276\216\346\225\260\357\274\210\347\256\200\345\215\225\357\274\211.md" @@ -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 原题链接和其他优选题解。 +