Simple C imlpementation with O(1) memory


  • 0
    T
    int sos(int n) {
    	int res = 0;
    	while (n) {
    		res += ((n % 10)*(n % 10));
    		n /= 10;
    	}
    
    	return res;
    }
    
    bool isHappy(int n) {
    	char map[250] = { 0 };
    	// After 1 sos maximum sum can not be larger than 243
    	if ((n = sos(n)) == 1)
    		return true;
    
    	while (!map[n = sos(n)]) {
    		map[n] = 1;
    		if (n == 1)
    			return true;
    	}
    
    	return false;
    }

Log in to reply
 

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