leetCode(29):Happy Number
2015-07-03 07:55
489 查看
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
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) { map<int,int> value; while(n!=1) { value =1; int remain; int tmp=0; while(n!=0) { remain=n%10; tmp+=remain*remain; n=n/10; } n=tmp; if(value.find(n)!=value.end()) break;//是否已经出现过 } if(n!=1) return false; return true; } };
相关文章推荐
- leetCode(29):Happy Number 分类: leetCode 2015-07-03 07:55 141人阅读 评论(0) 收藏
- Android快速开发之appBase——实战《购物车》
- android在线浏览图片
- Android之NDK开发
- Android图片浏览
- Android.mk 变量解释
- Android-ndk中的.mk文件语法规则
- Android下修改hosts文件-重启有效
- gradle DSL method not found: android()错误解决办法
- 再造轮子之网易彩票-第二季(IOS 篇 by sixleaves)
- Swift基本语法
- Android Mms之:深入理解对话列表管理
- Android判断软键盘弹出并隐藏的简单完美解决方案
- Android 控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
- 在cocos2d-x中实现真随机数
- Android 仿微信Activity进入退出动画(右进右出动画 )
- 发布app的电脑换了,发布证书怎么办
- android-training-course-in-chinese
- WebView加载本地html无法显示CSS样式 。
- setOnClickListener()与报错OnClickListener()原因