Problem:

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

Summary:

若一个整数的各个位数的平方和相加,不断重复这个操作后为1,则这个数为happy number。给出一个数n,判断其是否为happy number。

Analysis:

一个数重复各个位数平方和相加的操作为1时为happy number,那么判断一个数不是happy number的依据,则为各个位数平方和相加无论多少次,依旧不能为1。那么一定是在相加的过程中出现了数的循环,1不含在循环中。

所以只需重复平方和操作,判断每个数的各个位数平方和计算完毕后所得结果有无出现过,若从未出现,则继续计算,否则判断此数不是happy number。

 class Solution {
public:
bool isHappy(int n) {
unordered_map<int, int> m;
while (true) {
int sum = ;
while (n) {
sum += (n % ) * (n % );
n /= ;
} n = sum;
if (m[sum]++ > ) {
break;
}
} return n == ;
}
};

LeetCode 202 Happy Number的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  5. (easy)LeetCode 202.Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  7. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  8. C#版(打败97.89%的提交) - Leetcode 202. 快乐数 - 题解

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

  9. leetcode 202

    202. Happy Number Write an algorithm to determine if a number is "happy". A happy number i ...

随机推荐

  1. 【C语言入门教程】2.6 运算符

    运算符是程序中用于数值运算的操作符,C 语言的运算符可分为 算术运算符.关系与逻辑运算符 和 位操作运算符 这 3 类. 2.6.1 算术运算符 算术运算符用来完成基本的数值运算,如 加.减.乘.除, ...

  2. 第21天 fastlane

    [投稿]使用 fastlane 实现 iOS 持续集成 http://www.cocoachina.com/ios/20150916/13433.html

  3. PHPCMS系统使用的弹出窗口插件artDialog

    来源: http://aui.github.io/artDialog/doc/index.html  (官方) http://lab.seaning.com/ http://www.mb5u.com/ ...

  4. Vim编辑器运用的五个技巧

    导读 如今 Vim 是每个人最喜欢的 Linux 文本编辑器,也是开发者和系统管理者最喜爱的开源工具.大多数人只是熟悉Vim的最最基本的操作,只能在终端使用 Vim 修改文本,但是它并没有任何一个我想 ...

  5. 剑指Offer 反转链表

    题目描述 输入一个链表,反转链表后,输出链表的所有元素.     思路: 法1:用栈,压栈出栈 法2:头插法(有递归非递归2中)   AC代码: /* struct ListNode { int va ...

  6. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  7. Ubuntu14.04安装intel集显驱动

    Ubuntu14.04安装intel集显驱动 标签(空格分隔): ubuntu linux 驱动安装 1.查看本机显卡型号 使用lspci命令来获取PCI接口硬件信息 o@o-pc:~$ lspci ...

  8. java文件和文件夹复制、删除、移动操作

    java文件和文件夹复制.删除.移动操作 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputS ...

  9. 使用jar命令打war包

    1.打开cmd进入web项目发布文件夹 2.,输入jar -cvf qxpt.war * (*表示当前目录下所有子目录) 3,回车等待执行完成就可以了 4.如果web项目发布文件夹有多个文件夹,而打w ...

  10. 谷歌chrome浏览器和火狐firefox浏览器自带http抓包工具和请求模拟插件

    谷歌chrome浏览器自带http抓包工具 chrome://net-internals/ 谷歌chrome浏览器http请求模拟插件:postman 火狐http请求模拟插件:httprequest ...