7ms JAVA solution


  • 0
    V

    public class Solution {

    public boolean isHappy(int n) {
        Set<Integer> visited = new HashSet<Integer>();
        if(n == 1) {
            return true;
        }
        while(visited.add(n)) {
            int m = 0;
            while(n > 0) {
                m += Math.pow(n%10, 2);
                n /= 10;
            }
            n = m;
            
            if(n == 1) {
                return true;
            }
        }
        
        return false;
    }
    

    }


Log in to reply
 

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