[LeetCode] Happy Number
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
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的更多相关文章
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 力不从心 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 ...
- [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 ...
- [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 ...
- [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), ...
- LeetCode#476 Number Complement - in Swift
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
随机推荐
- html之左边不动右边内容自动修剪并出现滚动轮查看剩余内容
<html lang="en"> <head> <meta charset="UTF-8"> <title>Ti ...
- Java微博搜索关键字采集
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- PDF.js
http://www.linuxidc.com/Linux/2015-06/118728.htm http://blog.csdn.net/xiangcns/article/details/42089 ...
- oracle用户创建及权限设置及表空间
建立表空间: create tablespace portx_data datafile 'D:\oracle_data\portx.dbf' size 50m autoextend on next ...
- 解读Unity中的CG编写Shader系列三
转自http://www.itnose.net/detail/6096068.html 在上一个例子中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上.这篇文章 ...
- unity3D技术之事件函数的执行顺序[转]
unity3D技术之事件函数的执行顺序 转自http://www.yxkfw.com/?p=13703 在unity的脚本,有大量的脚本执行按照预先确定的顺序执行的事件函数.此执行顺序说明如下: ...
- Tomcat服务器绑定域名的配置
前面写到过Linux下tomcat服务器的部署,实际上只要域名正常解析到了服务器,那么不用绑定域名也是可以正常访问的,比如默认情况下访问xxx.net:8080与www.xxx.net:8080都可以 ...
- 9.SpringMVC和json结合传递参数
input的值一定要用.attribute来取值.val( )只能用可以看看开源社区jQuery的ajax请求.html():读取和修改一个元素的HTML内容,详情.html():.text():读取 ...
- 解析客户端IP
<html><head><title>新浪IP解析接口的使用</title><metahttp-equiv=Content-Typecontent ...