My code return true for 13 input, but test case is failing by showing that output is false


  • -1
    M
    #include <uthash.h>
    

    typedef struct my_hlist
    {
    int n;
    UT_hash_handle hh;
    }hlist;

    hlist *list = NULL;
    hlist *node;

    bool isHappy(int n) {
    int sum=0;

    while (1)
    {
    sum = 0;
    while (n)
    {
    sum += (n%10)*(n%10);
    n= n/10;
    }
    n = sum;
    if(sum == 1)
    return true;

       node = NULL;
       HASH_FIND_INT(list,&sum,node);
       if(node)
         return false;  
         
       node = malloc(sizeof(hlist));
       node->n = sum;
       HASH_ADD_INT(list,n,node);         
    }
    

    }


  • 0
    B

    You never return for non-happy elements...
    You should return false if 1<sum<10


Log in to reply
 

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