class Solution {
public:
/**
* @param n an integer
* @return true if this is a happy number or false
*/
#include <string>
bool isHappy(int n) {
// Write your code here
vector<int> happy;
int temp = n;
while (1)
{
happy.push_back(temp);
temp = happyNum(temp);
if (temp == 1)
{
return true;
}
for(int i = 0; i < happy.size(); i++)
{
if (happy[i] == temp)
{
return false;
}
}
}
}
int happyNum(int num)
{
int sum = 0;
while (num > 0) {
sum += (num % 10) * (num % 10);
num = num / 10;
}
return sum;
}
};
判断是否是快乐数
最新推荐文章于 2024-09-19 01:00:00 发布