Java Recursive Solution with ArrayList


  • 1
    A
    public class Solution {
    
    ArrayList<Integer> array = new ArrayList<Integer>();
    
    public boolean isHappy(int n) {
        if(n == 1) {
            return true;
        }
        if(array.contains(n)) {
            return false;
        }
        array.add(n);
        int sum = 0;
        int elem;
        while(n != 0) {
            elem = n % 10;
            n = n / 10;
            sum += elem * elem;
        }
        return isHappy(sum);
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.