Happy Number

Total Accepted: 35195 Total Submissions: 106936 Difficulty: Easy

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.

class Solution {
public:
bool isHappy(int n) {
if (n < ) return false;
unordered_set<int> loop;
while (true) {
if (n == ) return true;
int t = n;
int round = ;
while (t > ) {
int p = t % ;
round += p * p;
t /= ;
} if (loop.find(round) != loop.end()) return false;
else loop.insert(round);
n = round;
}
return true;
}
};

这题用JS 就不好搞了,因为运算老是变成浮点数。。。思路比较简单,同样使用哈希表来监测loop。

[LeetCode] Happy Number的更多相关文章

  1. C#版 - Leetcode 191. Number of 1 Bits-题解

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

  2. [leetcode]200. Number of Islands岛屿个数

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  3. [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  4. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  6. 力不从心 Leetcode(ugly number heap) 263, 264,313

    Leetcode ugly number set (3 now) new ugly number is generated by multiplying a prime with previous g ...

  7. [LeetCode] 200. Number of Islands 岛屿的数量

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. [LeetCode] 305. Number of Islands II 岛屿的数量 II

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  9. [LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  10. LeetCode#476 Number Complement - in Swift

    Given a positive integer, output its complement number. The complement strategy is to flip the bits ...

随机推荐

  1. PyCharm 5 破解注册方法

    方法: 调整时间到2038年. 申请30天试用 退出pycharm 时间调整回来即可. 或者: 注册时选择 License server ,填 http://idea.lanyus.com ,然后点击 ...

  2. 表单验证神器——jquery.validate插件

    jquery.validate.js插件应用举例,ajax方式提交数据. html代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...

  3. COGS 2416.[HZOI 2016]公路修建 & COGS 2419.[HZOI 2016]公路修建2 题解

    大意: [HZOI 2016]公路修建 给定一个有n个点和m-1组边的无向连通图,其中每组边都包含一条一级边和一条二级边(连接的顶点相同),同一组边中的一级边权值一定大于等于二级边,另外给出一个数k( ...

  4. thinkphp save()方法没有数据,保存失败解决办法

    thinkphp save()方法没有数据保存返回0,保存失败返回false   可以对返回值判断一下就好 $ret = $model->save($data); //var_dump($ret ...

  5. python gui之tkinter界面设计pythonic设计

    ui的设计,控件id的记录是一件比较繁琐的事情. 此外,赋值和读取数据也比较繁琐,非常不pythonic. 有没有神马办法优雅一点呢?life is short. 鉴于控件有name属性,通过dir( ...

  6. scp和rsync限制传输速度

    1.scp 限速100KB/s scp -l 1000 test root@192.168.1.104 此时的传输速率就是1M/8=100KB左右 2.rsync 限速100KB/s rsync -a ...

  7. ThinkPHP在Ubuntu上 : mkdir的使用

    问题: 环境:Ubuntu系统,xampp1.8.3-5建站工具 描述:以root的身份运行.Apache的 DocumentRoot = "/var/www" .thinkphp ...

  8. poj 2240(floyd)

    http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...

  9. INNODB自增主键的一些问题

    背景: 自增长是一个很常见的数据属性,在MySQL中大家都很愿意让自增长属性的字段当一个主键.特别是InnoDB,因为InnoDB的聚集索引的特性,使用自增长属性的字段当主键性能更好,这里要说明下自增 ...

  10. ffmpeg-20160517-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...