码迷,mamicode.com
首页 > 移动开发 > 详细

Happy Number

时间:2015-07-08 12:21:17      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

https://leetcode.com/problems/happy-number/

快乐数

 1 class Solution {
 2 public:
 3     bool isHappy(int n) {
 4         int res;
 5         int temp;
 6         unordered_set<int> us;
 7         us.insert(n);
 8         while(true)
 9         {
10             res=0;
11             do
12             {
13                 temp=n%10;
14                 res+=(temp*temp);
15                 n/=10;
16             }while(n!=0);
17             if(res==1)
18                 return true;
19             if(us.find(res) != us.end())
20                 return false;
21             n=res;
22             us.insert(n);
23         }
24     }
25 };

 

Happy Number

标签:

原文地址:http://www.cnblogs.com/aguai1992/p/4629566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!