12 numbers matters(C, 3ms, time O(1), mem O(1))


  • 0
    H

    I found that in the range from 1 to 99, only these 12 numbers can transform to 1;
    And for an Integer, biggest value can be transformed is lower that 810(9910), transform again, the value decrease.

    class Solution {
    public:
        bool isHappy(int n) {
            int ha[] = {1, 68, 86, 13, 31, 82, 28, 19, 91, 23, 32, 44};
            int i;
            for(int j = 0; j < 8; j++){
                i = 0;
                for(; i < sizeof(ha)/sizeof(int); i++){
                    if(ha[i] == n) return true;
                }
                int next = 0, tmp = n;
                while(tmp != 0){
                    next += ((tmp%10) * (tmp%10));
                    tmp /= 10;
                }
                n = next;
            }
            return false;
        }
    };
    

  • 0
    H

    @hanjianqiao 810(9X9X10)


  • 0
    J

    @hanjianqiao
    I have a question, why you think 8 times is ok?


Log in to reply
 

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