My java solution


  • 1
    N
    public class Solution {
    
    int[] arr = new int[10000];
    int count = -1;
    
    public boolean isHappy(int n) {
        
        int next = 0;
        
        while(n!=1){
            
            while(n!=0){
                next = next + (n%10)*(n%10);
                n=n/10;
            }
            
            if(isInArr(next, arr, count))
                return false;
                
            arr[++count]=next;
            n = next;
            next=0;
            
        }
        
        return true;
    }
    
    
    public boolean isInArr(int n , int[] a, int count){
        
        for(int i=0; i<=count; i++)
            if(a[i]==n)
                return true;
        
        return false;
    }
    

    }


Log in to reply
 

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