版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址
http://blog.csdn.net/lzuacm

C#版 - Leetcode 202. 快乐数 - 题解

Leetcode 202.Happy Number
在线提交: https://leetcode-cn.com/problems/happy-number/

LintCode 488 https://www.lintcode.com/problem/happy-number/

题目描述


编写一个算法来判断一个数是不是“快乐数”。

一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。

示例:

输入: 19
输出: true

解释:
<nobr aria&#45;hidden="true"> 12 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 1 </mn> <mn> 2 </mn> </msup> </math> + <nobr aria&#45;hidden="true"> 92 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 9 </mn> <mn> 2 </mn> </msup> </math> = 82
<nobr aria&#45;hidden="true"> 82 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 8 </mn> <mn> 2 </mn> </msup> </math> + <nobr aria&#45;hidden="true"> 22 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 2 </mn> <mn> 2 </mn> </msup> </math> = 68
<nobr aria&#45;hidden="true"> 62 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 6 </mn> <mn> 2 </mn> </msup> </math> + <nobr aria&#45;hidden="true"> 82 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 8 </mn> <mn> 2 </mn> </msup> </math> = 100
<nobr aria&#45;hidden="true"> 12 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 1 </mn> <mn> 2 </mn> </msup> </math> + <nobr aria&#45;hidden="true"> 02 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 0 </mn> <mn> 2 </mn> </msup> </math> + <nobr aria&#45;hidden="true"> 02 </nobr> <math xmlns="http&#58;&#47;&#47;www&#46;w3&#46;org&#47;1998&#47;Math&#47;MathML"> <msup> <mn> 0 </mn> <mn> 2 </mn> </msup> </math> = 1



思路:
迭代地求给定数的各位数字的平方和,维护一个set,迭代循环的出口是平方和为1或已在set中出现过。

也可当成数学问题来做,比如OEIS(用于收录整数数列规律的在线百科)网站中有介绍:
http://oeis.org/wiki/Lucky_numbers

已AC代码:

public class Solution
{
    public bool IsHappy(int n)
    {
        HashSet<int> unhappy = new HashSet<int>();
        while (!unhappy.Contains(n) && n != 1)
        {
            unhappy.Add(n);
            n = GetSquareSum(n);
        }
        return n == 1;
    }

    private int GetSquareSum(int n)
    {
        int sum = 0;
        while (n > 0)
        {
            var r = n - (n/10) * 10; // n%10
            n = n / 10;
            sum += r*r;            
        }
        return sum;
    }
}

Rank:
You are here!
Your runtime beats 97.89 % of csharp submissions.

使用%取模,会降低效率,结果显示 Your runtime beats 85.21 % of csharp submissions.

更简洁的写法:

public class Solution
{
    public bool IsHappy(int n)
    {
        HashSet<int> unhappy = new HashSet<int>();
        while (n != 1 && !unhappy.Contains(n))
        {
            unhappy.Add(n);
            int sum = 0;
            while (n != 0)
            {
                var r = n - (n / 10) * 10;
                sum += r * r;
                n /= 10;
            }
            n = sum;
        }
        return n == 1;
    }
}