题目:
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
Note: The input number n will not exceed 100,000,000. (1e8)

思路:
如果num是1,则直接返回false,定义一个用来存储约数的unordered_set的res,因为如果不是1,那么1一定是它的约数,将其插入到ret中,i从2开始取到num的平方根,如果num能整除i,且res中没有i,将i和num/i都放到res里。再将res中的数都相加赋给result,如果num和result相等则返回true;否则返回false。

代码:

class Solution {
public:bool checkPerfectNumber(int num) {if(num==1){//如果num是1,则直接返回falsereturn false;}unordered_set<int> res;//用来存储约数的unordered_setres.insert(1);//因为如果不是1,那么1一定是它的约数int high=sqrt(num);//因为只要计算到num的平方根,就可以取得所有的约数for(int i=2;i<=high;++i){//注意,先计算平方根,将值作为边界值比sqrt(num)直接作为边界值效率高if(num%i==0&&!res.count(i)){//如果num能整除i,且res中没有ires.insert(i);//将i和num/i都放到res里res.insert(num/i);}}int result=0;//将res中的数都相加赋给resultfor(unordered_set<int>::iterator iter=res.begin();iter!=res.end();++iter){result+=*iter;}return num==result;//如果num和result相等则返回true;否则返回false}
};

**输出结果:**3ms(如果将sqrt(num)直接作为边界值 ,则结果为6ms,慢了一半)

LeetCode 507. Perfect Number相关推荐

  1. Leetcode——507. Perfect Number

    题目原址 https://leetcode.com/problems/perfect-number/description/ 题目描述 We define the Perfect Number is ...

  2. 507. Perfect Number

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

  3. 507. Perfect Number(Python)

    参考:https://www.cnblogs.com/grandyang/p/6636879.html

  4. C#LeetCode刷题之#507-完美数(Perfect Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3879 访问. 对于一个 正整数,如果它和除了它自身以外的所有正因 ...

  5. java perfect number_LeetCode算法题-Perfect Number(Java实现)

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

  6. LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...

  7. I00035 完美数(Perfect number)

    笛卡尔说:"能找出的完美数是不会多的,好比人类一样,要找一个完美人亦非易事." 数论中,若一个正整数除了本身之外所有因子之和正好等于此数自身,则称此数为完美数.完美数(Perfec ...

  8. 【整数转字符串】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...

  9. 【To Do! 重点 正则表达式】LeetCode 65. Valid Number

    LeetCode 65. Valid Number 本博客转载自:[1]http://www.cnblogs.com/yuzhangcmu/p/4060348.html [2]https://blog ...

最新文章

  1. html body onunload,html页面的onunload、onbeforeunload
  2. 移动端字体单位该使用px还是rem?
  3. 【github技巧】解决githubusercontent打不开的问题
  4. 学习ASP.NET Core,你必须了解无处不在的“依赖注入”
  5. TextRank算法原理和提取关键词的主要过程详解 计算句子相似度 计算句子重要性公式
  6. cdr非法软件 您的产品已被禁用怎么回事_多层刺网非法捕鱼 顺庆男子被取保候审...
  7. 完美收官!java程序内存溢出找原因
  8. 怒肝三个月啃完这110道面试题,跳槽薪资翻倍
  9. 念念不忘,必有回响!6月更文活动的一些总结
  10. Mplayer 的编译
  11. Kubernetes详解(十九)——Kubernetes Pod控制器
  12. 《C#高效编程》读书笔记11-理解短小方法的优势
  13. win10计算器rsh_Win10计算器快捷键
  14. 波士顿房价预测实验报告
  15. Spring In Action 4 学习笔记(一)Spring概览
  16. MySQL篇【5】——约束
  17. excel计算机不准确,excel表格数据合计不准确-EXCEL表格中,求和的数字总是不对...
  18. 关于U盘装系统的那点破事儿---No bootable device found
  19. 2019计算机电源海关征税,回国华人注意!2019中国海关严打:这些东西千万别带了!...
  20. 相机参数和相机模型各个坐标系(世界坐标系、相机坐标系、图像坐标系、像素坐标系之间变换)

热门文章

  1. 弹性碰撞后速度方向_IPC: SIGGRAPH 2020开源有限元碰撞独家处理方案
  2. Python3 sorted() 与sort()函数
  3. linux版本和目录结构
  4. tensorflow2.0及以上版本在使用Session模块时报错:AttributeError: module ‘tensorflow‘ has no attribute ‘Session‘ 已解决
  5. “此网站使用的安全性配置已过时,这可能会导致您的信息(例如密码、消息或信用卡卡号)”
  6. 骗子收录网站源码PHP搭建
  7. 开源Blog系统-欧式风格家具网站源码v1.5.4
  8. 某页纸api接口网站源码 高速php源码
  9. html5拨动密码锁游戏,密码锁游戏(JavaScript)
  10. 直播短视频手机APP应用下载页面静态html网页模板