Why 13 would be wrong answer, it says my code would return false


  • 0
    I

    #include<stdio.h>
    #include<stdlib.h>

    int num[1000] = {0};
    int len = 0;

    int exist(int n){
    for(int i = 0;i < len;i++)
    if(num[i] == n)
    return 1;
    num[len] = n;
    len ++;
    return 0;
    }

    bool isHappy(int n) {
    int result = 0;
    if(n == 0)
    return false;

    while(1){
    	while(n>0){
    		result = result+(n%10)*(n%10);
    		n = n/10;
    	}
    	if(result == 1)
    		return true;
    	else if(exist(result))
    		return false;
    	else {
    		n = result;
    		result = 0;
    	}
    }
    

    }


Log in to reply
 

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