```
public class Solution {
public boolean isHappy(int n) {
ArrayList<Integer> visited = new ArrayList<Integer>();
while(n!= 1) {
if(visited.contains(n)) {
return false;
}
visited.add(n);
int tmp = 0;
while(n!=0) {
int dig = n%10;
tmp += dig*dig;
}
n = tmp;
}
return true;
}
}
```