My C# Solution


  • 0
    F
    public class Solution {
        public bool IsHappy(int n) {
            List<int> list = new List<int>();
            while(n != 1)
            {
                n = getSum(n);
                if (list.Contains(n))
                {
                    return false;
                }
                else
                {
                    list.Add(n);
                }
            }
            return true;
        }
        
        private static int getSum(int n)
        {
            int sum = 0;
            while (n != 0)
            {
                sum += (n % 10) * (n % 10);
                n /= 10;
            }
            return sum;
        }
    }
    

Log in to reply
 

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