[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/mJackie/leetcode/master/code/lc202.java [Back]  [Original]

package code;
/*
 * 202. Happy Number
 * 1Happy Number
 * Easy
 * Hash Table, Math
 * 1hashset
 *      
 * Tips
 */
import java.util.HashSet;

public class lc202 {
    public boolean isHappy(int n) {
        HashSet hs = new HashSet();
        hs.add(n);
        int sum = 0;
        while(true){
            sum = 0;
            while(n!=0){
                sum += Math.pow(n%10,2);
                n = n/10;
            }
            if(sum==1) return true;
            else if(hs.contains(sum)) break;
            hs.add(sum);
            n = sum;
            System.out.println(sum);
        }
        return false;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page