public class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<Integer>();
while (n != 1) {
int temp = n;
int sum = 0;
while(temp != 0) {
int digital = temp % 10;
sum += digital * digital;
temp /= 10;
}
if(!set.add(sum)) return false;
n = sum;
}
return true;
}
}
int digitSquareSum(int n) {
int sum = 0, tmp;
while (n) {
tmp = n % 10;
sum += tmp * tmp;
n /= 10;
}
return sum;
}
bool isHappy(int n) {
int slow, fast;
slow = fast = n;
do {
slow = digitSquareSum(slow);
fast = digitSquareSum(fast);
fast = digitSquareSum(fast);
} while(slow != fast);
if (slow == 1) return 1;
else return 0;
}
public class Solution {
public boolean isHappy(int n) {
if(n==1||n==7||n==10) return true;
if(n<10) return false;
int sum=(int)Math.pow(n%10,2);
while(n>=10){
n/=10;
sum+=(int)Math.pow(n%10,2);
}
return isHappy(sum);
}
}