javascript solution with hashtable


  • 0
    H

    var isHappy = function(n) {

    var sum=0;
    var m;
    var hashTable ={};
    while(n){
        
        while(n!= 0){
          
           m=n%10;
           n=parseInt(n/10);
           sum=sum+m*m;  
        }
         if (sum == 1){
             return true;
         }
         if(!hashTable[sum]){
             hashTable[sum]=1;
         } else{
             return false;
         }
         n=sum;
         sum=0;
         
    }
    

    };


Log in to reply
 

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