传送门

202. Happy Number

My Submissions

Question

Total Accepted: 56706 Total Submissions: 158441 Difficulty: Easy

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags

Hash Table Math

Show Similar Problems

按公式计算,判断一个数是不是happy number
 1 class Solution {
 2 public:
 3     bool isHappy(int n) {
 4         if(n < 0) return false;
 5         map<int,int> mp;
 6         while(n != 1){
 7             if(mp[n] == 1) return false;
 8             mp[n] = 1;
 9             n = calculate(n);
10         }
11         return true;
12     }
13     int calculate(int n)
14     {
15         int ret = 0;
16         int temp = 0;
17         while(n){
18             temp = n % 10;
19             ret += (temp * temp);
20             n /= 10;
21         }
22         return ret;
23     }
24 };

转载于:https://www.cnblogs.com/njczy2010/p/5227607.html

leetcode 202. Happy Number相关推荐

  1. [LeetCode]202. Happy Number(平衡二叉树 哈希表)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  2. python leetcode 202. Happy Number

    类似于链表环中快慢指针的思想 class Solution(object):def isHappy(self, n):""":type n: int:rtype: boo ...

  3. leetcode 202.Happy Number (python3 )

    题目: 题目分析:首先,本题需要判定输入的整数其各位数平方求和最终值是否为1 ?输入整数位整型,返回值为布尔型. 编程思路: 1.需要利用到循环实现每一次求和后判定是否为1 . 2.需要排除有可能出现 ...

  4. LeetCode 202. Happy Number--Python解法--数学题

    此文首发于我的个人博客:LeetCode 202. Happy Number–Python解法–数学题 - zhang0peter的个人博客 LeetCode题解专栏:LeetCode题解 LeetC ...

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

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

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

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

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

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

  8. 【回文串2】LeetCode 9. Palindrome Number

    LeetCode 9. Palindrome Number Solution1:我的答案 思路一:转化为字符串 class Solution { public:bool isPalindrome(in ...

  9. 【?异或】LeetCode 260. Single Number III

    LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...

最新文章

  1. 关于equals与hashcode的重写
  2. centos 6.5安装mysql5.7,centos6.5安装mysql5.7
  3. 基于Docker的GoldenGate部署
  4. hdu 5813 Elegant Construction
  5. Educational Codeforces Round 81 (Rated for Div. 2) E. Permutation Separation 线段树 + dp
  6. php authorization,PHP CURL设置Authorization
  7. 前端工程师需要学习ps 吗_转行学习web前端开发,需要哪些工具和需要学习什么?...
  8. 习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994, UVa232)
  9. Android Binder Debug
  10. wildfly access log 开启
  11. directsound播放32位float类型的pcm格式音频
  12. 【一年总结】记我的大一生活
  13. 计算机系统基础书记,【盘点】开学第一周:学风浓 教风严 校风正
  14. 基于MATLAB的烟雾火灾检测识别系统
  15. 王自如评价鸿蒙OS,王自如发表致歉声明 测评将加利益相关说明
  16. FXTZ 1.10 小爱使用
  17. 内蒙古大学计算机学院周建涛,刘靖(内蒙古大学副教授)_百度百科
  18. matlab排序函数——sort
  19. VS2017使用NuGet安装Oracle.ManagedDataAccess.EntityFramework 的异常解决
  20. BUAA数据结构第四次作业2023

热门文章

  1. setfacl 权限导出_Linux如何使用setfacl命令创建权限文件
  2. unity3d 不规则外发光描边_Shader案例之内发光和边缘泛光效果
  3. 管理服务器一般的作用,管理服务器作用
  4. 消息队列mysql redis那个好_Redis作为消息队列与RabbitMQ的比较
  5. mysql文本自动递增_mysql-如何创建自动递增的字符串?
  6. a73*2+a53*2指的是什么_什么言四字词
  7. pytest teardown 未执行_python3+pytest+allure框架搭建之pytest详解(一)
  8. fcn网络训练代码_用FCN做分割
  9. mongoDB如何将数据导成csv文件?
  10. linux看是否连上服务器