C#LeetCode刷题之#507-完美数(Perfect Number)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。
对于一个 正整数,如果它和除了它自身以外的所有正因子之和相等,我们称它为“完美数”。
给定一个 正整数 n, 如果他是完美数,返回 True,否则返回 False
输入: 28
输出: True
解释: 28 = 1 + 2 + 4 + 7 + 14
注意:输入的数字 n 不会超过 100,000,000. (1e8)
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.
Input: 28
Output: True
Explanation: 28 = 1 + 2 + 4 + 7 + 14
Note: The input number n will not exceed 100,000,000. (1e8)
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。
public class Program {
public static void Main(string[] args) {
var num = 6;
var res = CheckPerfectNumber(num);
Console.WriteLine(res);
num = 496;
res = CheckPerfectNumber2(num);
Console.WriteLine(res);
Console.ReadKey();
}
private static bool CheckPerfectNumber(int num) {
//28 = 1 + 2 + 4 + 7 + 14
//以根号 28 = 5.29 为分水岭
//若 2 能被 28 整除,28 / 2 = 14
//则 2 和 14 都需要计入计算和的范围之内
//边界处理
if(num == 1) return false;
//1 总是因子,提前 + 1
var sum = 1;
//从 2 到 Math.Sqrt(num)
for(var i = 2; i <= Math.Sqrt(num); i++) {
//若整除,则被除数和商均计入累计和
if(num % i == 0) {
sum += i + num / i;
}
}
//满足完美数定义时返回true,否则返回false
return sum == num;
}
private static bool CheckPerfectNumber2(int num) {
//题目范围内只包含这 5 个完美数
//套用强哥的一句话,我从未见过如此厚颜无耻之人
return num == 6 || num == 28 ||
num == 496 || num == 8128 ||
num == 33550336;
}
}
以上给出2种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3879 访问。
True
True
分析:
显而易见,CheckPerfectNumber 的时间复杂度为: ,CheckPerfectNumber2 的时间复杂度为:
。
C#LeetCode刷题之#507-完美数(Perfect Number)的更多相关文章
- [Swift]LeetCode507. 完美数 | Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- leetcode刷题笔记-1. 两数之和(java实现)
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使 ...
- C#LeetCode刷题之#202-快乐数(Happy Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3856 访问. 编写一个算法来判断一个数是不是"快乐数& ...
- #leetcode刷题之路15-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- #leetcode刷题之路12-整数转罗马数字
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 ...
- #leetcode刷题之路29- 两数相除
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- #leetcode刷题之路1-两数之和
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符.返回被除数 dividend 除以除数 divisor 得到的商. 示例 1:输入: ...
- C#LeetCode刷题之#9-回文数(Palindrome Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...
- leetcode刷题第二天<两数相加>
题目描述 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...
随机推荐
- 深入理解JVM(③)再谈线程安全
前言 我们在编写程序的时候,一般是有个顺序的,就是先实现再优化,并不是所有的牛P程序都是一次就写出来的,肯定都是不断的优化完善来持续实现的.因此我们在考虑实现高并发程序的时候,要先保证并发的正确性,然 ...
- sql与SQL CODE和SQL State相关报错
操作数据库过程中,遇到许多问题,很多都与SQL CODE和SQL State相关,现在把一个完整的SQLCODE和SQLState错误信息和相关解释作以下说明,一来可以自己参考,对DB2错误自行找出原 ...
- 使用nvm安装node,运行node报错 node: command not found
1. 使用nvm安装node之后,直接运行node命令会报错 node: command not found 需要使用nvm ls 查询一下当前使用的安装的node版本,然后使用node use 版 ...
- Go Pentester - TCP Proxy
Building a TCP Proxy Using io.Reader and io.Writer Essentially all input/output(I/O). package main i ...
- OSCP Learning Notes - Buffer Overflows(3)
Finding Bad Characters 1. Find the bad charaters in the following website: https://bulbsecurity.com/ ...
- Python Ethical Hacking - Basic Concetion
What is Hacking? Gaining unauthorized access. Hackers? 1.Black-hat Hackers 2.White-hat Hackers 3.Gre ...
- less : 写一个display:flex的mixin
和scss一样,less也是一个好用的css预处理语言,语法也很相近. 而我们在使用display:flex的时候,很容易苦恼于里面的设置的单词很难记(尤其是对我这种英语很差的人来说). 所以我们可以 ...
- Kubernetes的10个基本事实!你知道几个?k8s与Docker又有何不同?
无论您是Kubernetes的新手还是只是想获得更多知识,这篇文章都会帮到您! Kubernetes是一个增长的趋势.近年来,K8s技术经历了从小型开源Google项目到Cloud Native Co ...
- Python3的一些基本输入输出
# python3 # 基本输入输出 #学会使用split()的使用是关键,input()的返回值一定是字符类型 .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. ...
- git chechout
在克隆完一个的版本库时,git会在本地创建一个master分支用于跟踪远端的master分支 如git clone abc.git 默认情况下git会在本地创建一个master分支 但是,在本地mas ...