My c solution couldn't pass if n=10,but same code can pass in c++


  • 0
    L
    int num[10000]={0};
    int m=0;
    bool isHappy(int n) {
        if(n==0) return false;
        if(n==1) return true;
        for(int i=0;i<m;i++){
            if(n==num[i]) return false;
        }
        num[m]=n;
        m++;
        int sum=0;
        int temp=0;
        while(n>0){
            temp=n%10;
            sum=(temp*temp)+sum;
            n=n/10;
        }
        if(sum==1) return true;
        isHappy(sum);
    }

  • 0
    J

    The last line should be

    return isHappy(sum);

    Didn't try it but looks like the rest is fine.


  • 0
    L

    no...thats not the problem...i tried.. still have the same problem...i don't think i need to return isHappy(sum). if thats the case...the problem would return runtime error...


Log in to reply
 

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