LeetCode: Happy Number
来源:互联网 发布:mac chmod 777 编辑:程序博客网 时间:2024/05/22 15:49
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
class Solution {public: bool isHappy(int n) { unordered_map<long, long> m_map; while(1) { long happyNum = getHappy(n); if(happyNum == 1 || happyNum == 0) return true; if(m_map.find(happyNum) != m_map.end()) { return false; } else { m_map[n] = happyNum; n = happyNum; } } }private: long getHappy(long n) { long result = 0; while(n != 0) { int digit = n%10; result += digit * digit; n /= 10; } return result; }};
0 0
- [leetcode] Happy Number
- leetcode: Happy Number
- [LeetCode] Happy Number
- [LeetCode]Happy Number
- LeetCode 202 Happy Number
- LeetCode OJ Happy Number
- [LeetCode]202.Happy Number
- LeetCode 202-Happy Number
- leetcode-202 Happy Number
- [LeetCode]Happy Number
- leetcode 202: Happy Number
- leetcode Happy Number
- [LeetCode 202]Happy Number
- LeetCode (24) Happy Number
- LeetCode Happy Number 题解
- [LeetCode] Happy Number
- leetcode 202 Happy Number
- LeetCode Happy Number
- 继承ViewGroup实现Scroll滑动效果
- Django URL调度
- 6.2.3.2 BLOB 和 TEXT 类型
- Shell读取用户输入【以及循环读取验证和while循环中添加多个条件[ .. ] &&或者|| [ .. ]】
- boost log 中打印源码的名字与行号的两种方法
- LeetCode: Happy Number
- 学习 Activiti5.13与springMVC整合,自学001期
- leetcode 日经贴,Cpp code -Contains Duplicate III
- Hibernate HelloWorld-12 HQL连接
- Unsupported major.minor version 51.0(jdk版本错误)
- svn下载网页代码
- 写给代码技术同事:被人信任才能有广阔未来
- 3个UIimageView实现图片的循环切换
- adb常用命令总结