1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| private Set<Integer> set=new HashSet(); private int getNext(int n){ int sum=0; while(n!=0){ int cur=n%10; n=n/10; sum+=cur*cur; } return sum; }
public boolean isHappy(int n) { while(n!=1){ if(set.contains(n)){ return false; }else{ set.add(n); } n=getNext(n); } return n==1; }
|