题目

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

Credits:

Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.

分析

此题目,只要理解了快乐数的判定条件,便很简单了。

快乐数(happy number)有以下的特性:在给定的进位制下,该数字所有数位(digits)的平方和,得到的新数再次求所有数位的平方和,如此重复进行,最终结果必为1。

以十进位为例:

2 8 → 2^2+8^2=68 → 6^2+8^2=100 → 1^2+0^2+0^2=1

3 2 → 3^2+2^2=13 → 1^2+3^2=10 → 1^2+0^2=1

3 7 → 3^2+7^2=58 → 5^2+8^2=89 → 8^2+9^2=145 → 1^2+4^2+5^2=42 → 4^2+2^2=20 → 2^2+0^2=4 → 4^2=16 → 1^2+6^2=37……

因此28和32是快乐数,而在37的计算过程中,37重覆出现,继续计算的结果只会是上述数字的循环,不会出现1,因此37不是快乐数。

不是快乐数的数称为不快乐数(unhappy number),所有不快乐数的数位平方和计算,最後都会进入 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 的循环中。

因此,只要循环计算过程中,结果为该数最初值或4,则必然为不快乐数。

AC代码

class Solution {
public:
bool isHappy(int n) {
if (n <= 0)
return false; if (n == 1)
return true;
int sum = n;
while (sum != 1)
{
int tmp = sum;
sum = 0;
//求各个位平方和
while (tmp != 0)
{
sum += pow(tmp % 10, 2);
tmp /= 10;
}//while if (sum == n || sum == 4)
return false;
}//while if (sum == 1)
return true;
else
return false;
}
};

GitHub测试程序源码

LeetCode(202) Happy Number的更多相关文章

  1. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  2. LeetCode(306) Additive Number

    题目 Additive number is a string whose digits can form additive sequence. A valid additive sequence sh ...

  3. LeetCode(65) Valid Number

    题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...

  4. LeetCode(260) Single Number III

    题目 Given an array of numbers nums, in which exactly two elements appear only once and all the other ...

  5. LeetCode(268) Missing Number

    题目 Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is missi ...

  6. LeetCode(136) Single Number

    题目 Given an array of integers, every element appears twice except for one. Find that single one. Not ...

  7. LeetCode(9)Palindrome Number

    题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could neg ...

  8. Leetcode(4)寻找两个有序数组的中位数

    Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O( ...

  9. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

随机推荐

  1. python库使用整理

    1. 环境搭建 l  Python安装包:www.python.org l  Microsoft Visual C++ Compiler for Python l  pip(get-pip.py):p ...

  2. Partition(线段树的离线处理)

    有一点类似区间K值的求法. 这里有两颗树,一个是自己建的线段树,一个是题目中给定的树.以线段树和树进行区分. 首先离散化一下,以离散化后的结果建线段树,线段树的节点开了2维,一维保存当前以当前节点为权 ...

  3. VS局域网断点调试设置

    1.电脑文档文件夹下\IISExpress\config文件内找到applicationhost.config文件编辑 找到<sites>节点 找到你要编辑的site节点 在<bin ...

  4. MySql数据库的相关操作

    SQL(Structred Query Language)结构化查询语言:和数据库交互的语言,进行数据库管理的语言. 一.数据库的操作: 1.查询所有数据库: show databases; 2.创建 ...

  5. Linux-软件安装(一) —— jdk/tomact 安装(普通安装)

    Linux-软件安装(一) -- jdk/tomact 安装(普通安装) 1. 可使用 FinalShell 上传至 Linux 服务器 2. 解压 cd /usr/local #解压命令 tar - ...

  6. MFC 消息中( WPARAM wParam,LPARAM lParam)包含信息

    windows的消息具有以下两个参数: (1)字参数(wParam) (2)长参数(lParam) 字参数和长参数都是32位整数,用于提供消息的附带消息,是消息传递过程中参数的载体.附加信息的消息号取 ...

  7. Java文件操作系列[2]——使用JXL操作Excel文件

    由于java流无法实现对Excel文件的读写操作,因此在项目中经常利用第三方开源的组件来实现.支持Excel文件操作的第三方开源组件主要有Apache的POI和开源社区的JXL. 总体来说,二者的区别 ...

  8. vue+element ui项目总结点(六)table编辑当前行、删除当前行、新增、合计操作

    具体属性方法参考官方网站:http://element-cn.eleme.io/#/zh-CN/component/installation <template> <div clas ...

  9. 10个优秀的移动Web应用开发框架

    在最近几年里,移动互联网高速发展.市场潜力巨大.继计算机.互联网之后,移动互联网正掀起第三次信息技术革命的浪潮,新技术.新应用不断涌现.今天这篇文章向大家推荐10大优秀的移动Web开发框架,帮助开发者 ...

  10. 图片,二进制,oracle数据库

    图片在oracle数据库中一般以二进制存在,存储类型是blob,然而clob类型一般存储的是大于4000的字符,不能用来存储图像这样的二进制内容,下面展示一下实现图像,二进制,oracle 数据库的应 ...