LeetCode | Happy Number

题目

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

分析与思路


得到每位数字的平方和比较简单,使用一个while语句就能实现,难点主要是对Happy Number性质的了解上。如果不是Happy Number,那么将会无限循环下去,此时该怎么办?
我的思路是这样的,首先非常容易就能判断出 在一位数字中 只有1和7是Happy Number,那么对一个数进行每位数字平方和的计算时,只要在循环中出现2 3 4 5 6 8 9中的一个,该数就不是Happy Number。按照这个思路生成的代码顺利通过了所有的测试,但是这里其实需要证明 在每个不是Happy Number的循环中,一定会在某次出现1~9中的一个数

代码为:

class Solution {
public:
    int sumOfTheSquaresOfItsDigits(int n)
    {
    	int sum=0;
    	while(n)
        {
            sum+=(n%10)*(n%10);
            n/=10;
        }
        return sum;
    }
    bool isHappy(int n) 
    {
    	if(n<=0)	return false;
    	while(n>10)
    		n=sumOfTheSquaresOfItsDigits(n);
    	if(n==1 || n==7 || n==10)
    		return true;
    	else 
    		return false;
    }
};

其实,上面的思路还有种变形,也即不需要到10,只要到7即可,2, 3 4 5 6都不是Happy Number,这样的思路写出的代码也顺利通过了所有测试样例。
代码:

bool isHappy(int n) {
    while(n>6)
    {
        int next = 0;
        while(n)
	{
		next+=(n%10)*(n%10); 
		n/=10;
	}
        n = next;
    }
    return n==1;
}
 
在Discuss区看到有人提出 所有不是Happy Number的数在cycle内都会出现4,所以把4作为了一个magic_number,他提供的代码为:
bool isHappy(int n) {
    if (n <= 0) return false;

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

但是!!!!以上的做法都面临一个问题,那就是需要给出严格的数学证明证明出: 在非Happy Number的每次计算自己数字平方和的过程中,一定会出现1~9、1~6、4。如果没有证明,其实还是不能严格说算法是正确的。在做题的时候我就没想到这点,但是所有测试用例都能通过。由于我数学是体育老师教的,我没办法给出严格的证明过程。所以我试着找一种其他方法解决这个问题。

int型整数最大值是 2147483647,也即所有数各个位上的数字平方和最大也不会超过9*9*10=810(10位数字全是9时),实际上第9位(从0位起算)最高位连3都不可能。所以可以开一个810个元素的数组(已经远远足够),对于出现过的数字, 就将相应的数组下标那个元素标志为true,问题就转换成一个简单的hash应用问题。

代码:

class Solution {
public:
    int sumOfTheSquaresOfItsDigits(int n)
    {
    	int sum=0;
    	while(n)
        {
            sum+=(n%10)*(n%10);
            n/=10;
        }
        return sum;
    }
    
    bool isHappy(int n)
    {
        bool hash[810];
        memset(hash,false,sizeof(hash));
    	int sum=n;
        while(true)
        {
            sum = sumOfTheSquaresOfItsDigits(sum);
            if(sum == 1)
                return true;
            else
            {
                if(hash[sum] == true)
                    return false;
                else
                    hash[sum] = true;
            }
        }
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值