A 8-Line 76 ms C# Solution


  • 0
    L
    public bool IsHappy(int n) {
        HashSet<int> hs = new HashSet<int>();
        for (int num = 0; n != 1; n = num, num = 0){
            foreach (char ch in n.ToString())
                num += (ch - '0') * (ch - '0');
            if (hs.Contains(num)) return false;
            else hs.Add(num);
        }
        return true;
    }

Log in to reply
 

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