//#202 Happy Number
//640ms 0%
class Solution {
public:
int addHappyVector(vector<int> v)
{
int sum(0);
for(unsigned int i=0; i<v.size(); i++)
{
sum = sum + pow(v[i], 2);
}
return sum;
}
bool isHappy(int n)
{
int i(0);
while(n != 1)
{
vector<int> digits = pushVector(n);
n = addHappyVector(digits);
i ++;
if(i > 2000) return false;
}
return true;
}
vector<int> pushVector(int num)
{
vector<int> digits;
while(num != 0)
{
int remainder = num % 10;
digits.push_back(remainder);
num = num / 10;
}
return digits;
}
};
[Leetcode]#202 Happy Number
最新推荐文章于 2024-09-08 20:02:08 发布