leetcode 202. Happy Number
传送门
202. Happy Number
My Submissions
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
Hash Table Math
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相关推荐
- [LeetCode]202. Happy Number(平衡二叉树 哈希表)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- python leetcode 202. Happy Number
类似于链表环中快慢指针的思想 class Solution(object):def isHappy(self, n):""":type n: int:rtype: boo ...
- leetcode 202.Happy Number (python3 )
题目: 题目分析:首先,本题需要判定输入的整数其各位数平方求和最终值是否为1 ?输入整数位整型,返回值为布尔型. 编程思路: 1.需要利用到循环实现每一次求和后判定是否为1 . 2.需要排除有可能出现 ...
- LeetCode 202. Happy Number--Python解法--数学题
此文首发于我的个人博客:LeetCode 202. Happy Number–Python解法–数学题 - zhang0peter的个人博客 LeetCode题解专栏:LeetCode题解 LeetC ...
- LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...
- 【整数转字符串】LeetCode 9. Palindrome Number
LeetCode 9. Palindrome Number Solution1: 不利用字符串 class Solution { public:bool isPalindrome(int x) {if ...
- 【To Do! 重点 正则表达式】LeetCode 65. Valid Number
LeetCode 65. Valid Number 本博客转载自:[1]http://www.cnblogs.com/yuzhangcmu/p/4060348.html [2]https://blog ...
- 【回文串2】LeetCode 9. Palindrome Number
LeetCode 9. Palindrome Number Solution1:我的答案 思路一:转化为字符串 class Solution { public:bool isPalindrome(in ...
- 【?异或】LeetCode 260. Single Number III
LeetCode 260. Single Number III Solution1: 博客转载自:http://www.cnblogs.com/grandyang/p/4741122.html 这道题 ...
最新文章
- 关于equals与hashcode的重写
- centos 6.5安装mysql5.7,centos6.5安装mysql5.7
- 基于Docker的GoldenGate部署
- hdu 5813 Elegant Construction
- Educational Codeforces Round 81 (Rated for Div. 2) E. Permutation Separation 线段树 + dp
- php authorization,PHP CURL设置Authorization
- 前端工程师需要学习ps 吗_转行学习web前端开发,需要哪些工具和需要学习什么?...
- 习题3-6 纵横字谜的答案(Crossword Answers, ACM/ICPC World Finals 1994, UVa232)
- Android Binder Debug
- wildfly access log 开启
- directsound播放32位float类型的pcm格式音频
- 【一年总结】记我的大一生活
- 计算机系统基础书记,【盘点】开学第一周:学风浓 教风严 校风正
- 基于MATLAB的烟雾火灾检测识别系统
- 王自如评价鸿蒙OS,王自如发表致歉声明 测评将加利益相关说明
- FXTZ 1.10 小爱使用
- 内蒙古大学计算机学院周建涛,刘靖(内蒙古大学副教授)_百度百科
- matlab排序函数——sort
- VS2017使用NuGet安装Oracle.ManagedDataAccess.EntityFramework 的异常解决
- BUAA数据结构第四次作业2023
热门文章
- setfacl 权限导出_Linux如何使用setfacl命令创建权限文件
- unity3d 不规则外发光描边_Shader案例之内发光和边缘泛光效果
- 管理服务器一般的作用,管理服务器作用
- 消息队列mysql redis那个好_Redis作为消息队列与RabbitMQ的比较
- mysql文本自动递增_mysql-如何创建自动递增的字符串?
- a73*2+a53*2指的是什么_什么言四字词
- pytest teardown 未执行_python3+pytest+allure框架搭建之pytest详解(一)
- fcn网络训练代码_用FCN做分割
- mongoDB如何将数据导成csv文件?
- linux看是否连上服务器