202. Happy Number

来源:互联网 发布:黄金单身汉 知乎 编辑:程序博客网 时间:2024/06/05 10:28

问题:
Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number

12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

我的解答:

class Solution {public:    bool isHappy(int n) {        int num=0;    int temp = n;    int temp_mul=0;    unordered_map<int, int>count;    count[n] = 1;    while (num!=1&&count[num]<2)    {        num = 0;        while (temp >= 1)        {             temp_mul = temp % 10;                       num += temp_mul*temp_mul;            temp /= 10;        }        temp = num;        count[num]++;    }    if (num == 1) return true;    else return false;    }};

8ms,beats 7.73%

解释:
这种方法很笨,就是一次次轮回地算,然后用哈希表存储每一次算出来的结果,假如重复了,造成轮回了,那么就不是happy number 。应该还有其他方法。

这是别人的做法1:

while (true) {            if (n == 1) { return true; }            if (n == 4) { return false; }            int next = 0;            while (n) { next += (n % 10) * (n % 10), n /= 10; }            n = next;        }    

解释:这种做法的原因是如果计算会进入一个循环轮回中的话,那么这个轮回必然是包含着计算出4这个数的。所以只要有4,那么就会说明这个不是happy number。

wiki中的数学证明和解释:

If n is not happy, then its sequence does not go to 1. Instead, it ends in the cycle:

4, 16, 37, 58, 89, 145, 42, 20, 4, … To see this fact, first note that if n has m digits, then the sum of the squares of its digits is at most 9^2 m, or 81m.

For m=4 and above,

n\geq10^{m-1}>81m so any number over 1000 gets smaller under this process and in particular becomes a number with strictly fewer digits. Once we are under 1000, the number for which the sum of squares of digits is largest is 999, and the result is 3 times 81, that is, 243.

In the range 100 to 243, the number 199 produces the largest next value, of 163. In the range 100 to 163, the number 159 produces the largest next value, of 107. In the range 100 to 107, the number 107 produces the largest next value, of 50. Considering more precisely the intervals [244,999], [164,243], [108,163] and [100,107], we see that every number above 99 gets strictly smaller under this process. Thus, no matter what number we start with, we eventually drop below 100. An exhaustive search then shows that every number in the interval [1,99] either is happy or goes to the above cycle.

The above work produces the interesting result that no positive integer other than 1 is the sum of the squares of its own digits, since any such number would be a fixed point of the described process.

There are infinitely many happy numbers and infinitely many unhappy numbers. Consider the following proof:

1 is a happy number, and for every n, 10n is happy since its sum is 1 and for every n, 2 × 10n is unhappy since its sum is 4 and 4 is an unhappy number.

wiki

以上这种方法很巧妙,利用了数学方法,然而在实际面试过程中,是不可能给数学推导的。所以下面用另一种方法。

class Solution {public:    bool isHappy(int n) {        int slow,fast;        slow=fast=n;        do{            slow = compute(slow);            fast=compute(fast);            fast=compute(fast);        }while(slow!=fast);        return 1==slow;    }private:    int compute(int n)    {        int num=0;        while(n)        {            num += (n%10) * (n%10);            n/=10;        }        return num;    }};

4ms

解释:List circle detect method,也是Floyd Cycle detection algorithm。
这种方法的思路是不断地计算,slow是每次走一步,fast是每次走两步,假如是到了1,那么fast会等slow。假如进入了一个环,那么由于fast每次走两步,slow走一步,那么肯定会有fast从后面追上slow的时候。
就判断是到了1而追上还是别的情况追上就知道了是不是环

0 0
原创粉丝点击