题目链接
解题思路: 两个指针,一快一慢,如果相遇,就会生成环 如果环内元素为1,那么就可以返回
解题思路:
1
class Solution { public: int get(int n){ int res = 0; while(n){ res += (n%10) * (n%10); n /= 10; } return res; } bool isHappy(int n) { int f = get(n); int s = n; while( f != s ){ f = get(get(f)); s = get(s); } return f==1; } };