Why Time Limit Exceeded in this way?


  • 0
    B

    Every unhappy number will fall in this loop:4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 ;

    public:
        bool isHappy(int n) {
            int next=0;
            while(n!=1&&n!=4&&n!=16&&n!=37&&n!=58&&n!=89&&n!=145&&n!=42&&n!=20){
                while(n){
                next+=(n%10)*(n%10);
                n/=10;}
                n=next;
            }
            return n==1;
        }
    };

Log in to reply
 

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