leetcode 202. Happy Number
2016-02-29 15:16
204 查看
传送门
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
202. Happy Number
My SubmissionsQuestion
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
class Solution { public: bool isHappy(int n) { if(n < 0) return false; map<int,int> mp; while(n != 1){ if(mp == 1) return false; mp = 1; n = calculate(n); } return true; } int calculate(int n) { int ret = 0; int temp = 0; while(n){ temp = n % 10; ret += (temp * temp); n /= 10; } return ret; } };
相关文章推荐
- android电池充电以及电量检测驱动分析
- Android控件— — —ImageView
- spring-jms的applicationContext.xml
- 隐藏android中EditText的下划线
- IOS开发 拍照上传图片旋转90°
- 详解Android中App的启动界面Splash的编写方法
- Android 使用百度鹰眼实现运动轨迹功能
- iOS推送
- android camera2 API流程分析
- Android 端微信就是无法进行登陆,但是检查代码都没有任何问题
- aFileChooser:兼容android2.1+的文件选择器
- Android基础核心总结之二-----Spinner、AutoCompleteTextView(自动完成控件)
- 自学android studio的一点小记录-----快捷键
- android学习日记01-搭配开发环境
- Android获取前台进程包名
- 自学android studio的一点小记录----svn共享过滤目录
- Android五大布局与实际应用详解
- 《2015互联网安全年报》,移动端成重灾区,黑灰产日益成熟
- iOS 性别封装
- Android studio 编译很慢解决方案