clueless coding // TODO: be smarter

LeetCode Happy Number: Java Solution



TL;DR Code Solution



public int getNext(int n) {
    int result = 0;
    while (n != 0) {
        int curr = n % 10;
        curr *= curr;
        result += curr;
        n /= 10;
    }
    return result;
}

public boolean isHappy(int n) {

    HashSet<Integer> set = new HashSet<Integer>();  

    while (n != 1) {

        if (set.contains(n)) {
            return false;
        }
        set.add(n);
        n = getNext(n);

    }

    return true;

}