My 2ms Code in C


  • 1
    C
       int calculate(int input){
           int tmp, ret = 0;
           while (input > 0){
               tmp = input % 10;
               input /= 10;
               ret += tmp * tmp;
           }
           return ret;
       }
        
        bool isHappy(int n) {
        	int tmp;
        	do{
        		tmp = calculate(n);
        		/**
        		 ** If n is an unhappy num,the final loop would be
                 ** 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4.
                 ** So here we just need to verify whether these number 
                 ** will appear in the calculate process 
                 **/
        		if (tmp == 4 || tmp == 16 || tmp == 37 || tmp == 42 || 
                    tmp == 20 || tmp == 58 || tmp == 89 || tmp == 145)
        			return false;
        		else
        			n = tmp;
        	} while (tmp != 1);
        	return true;
        }

Log in to reply
 

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