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

题目解答:不断计算num各位数字的平方和,直到进入循环或最终结果为1,并返回对应的结果。

代码如下:

class Solution {
public:
    bool isHappy(int n) {
        if(n <= 0)
            return false;
        unordered_set<int> num_set;
        while((n != 1) && (num_set.find(n) == num_set.end() ) )
        {
            num_set.insert(n);
            n = getSum(n);
        }
        if(n == 1)
        {
            return true;
        }
        else
            return false;
       
    }
   
    int getSum(int n)
    {
        int sum = pow((n % 10),2) ;
        while(n / 10 != 0)
        {
            n = n / 10;
            int tmp = pow((n % 10),2);
            sum += tmp;
           
        }
        return sum;
    }
};

时间: 2024-08-03 05:54:43

Leetcode题目:Happy Number的相关文章

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

Leetcode 数 Palindrome Number

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Palindrome Number Total Accepted: 12165 Total Submissions: 41736 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integ

LeetCode:Happy Number - 快乐数

1.题目名称 Happy Number(快乐数) 2.题目地址 https://leetcode.com/problems/happy-number/ 3.题目内容 英文: 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, repla

LeetCode 009 Palindrome Number

[题目] Determine whether an integer is a palindrome. Do this without extra space. [题意] 题意判断一个整数是否是回文数 注意一下几点: 1. 不能用额外的空间 2. 负数不是回文数 [思路1] 依次比较首位值,需要注意处理类似1002001这样的数,当比较完首位值之后余下的数变成200, 2之前的两个0会自动清除. [代码] class Solution { public: int _size(int x){ int

LeetCode 137 Single Number II(只出现一次的数字 II)(*)

翻译 给定一个整型数组,除了某个元素外其余的均出现了三次.找出这个元素. 备注: 你的算法应该是线性时间复杂度.你可以不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you im

leetcode题目思路以及部分解答(一)

为了进好公司这一个多月就得抽时间刷leetcode了..感觉这个OJ很不严谨...好多边界条件都没说清处..不过还好可以推测.唯一的好处就是不用自己编译调试,可以直接在网上显示出结果.当然,复杂一点的题目为了调试自己构建题目的结构也是很麻烦的...所以我发现提交里面错误好多.....再就是在笔记本上会时不时的变卡...每次提交都得等个3,4分钟才成功.要不就502错误... 我的题目按照通过率来.从通过率最高的题目开始讲解.每题不一定是最优解,都是我想得.仅供参考. 题目标题我都标好了.可以用c

LeetCode:Missing Number - 缺失的数字

1.题目名称 Missing Number (缺失的数字) 2.题目地址 https://leetcode.com/problems/missing-number 3.题目内容 英文:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n  find the one that is missing from the array. 中文:给出一个包含了n个不同数字的数组,从0开始一直到n,找出缺失的数字.如果数

LeetCode:Largest Number

1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetcode.com/problems/largest-number/ 3.题目内容 英文:Given a list of non negative integers, arrange them such that they form the largest number. 中文:给出一组非负整数,求这些非负整数可以拼接出的最大数字 说明:例如,给出数组 [3, 30, 34,