202. Happy Number (INT)
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
class Solution {
public:
int getSquare(int num){ //because square operation is very often, so enum the result
switch(num){
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
case : return ;
}
}
bool isHappy(int n) {
if(n == ) return false; set<int> squareSum;
return dfs(n, squareSum);
} bool dfs(int n, set<int>& squareSum){
int tmp;
int sum = ;
while(n){
tmp = n%;
sum += getSquare(tmp);
n /= ;
}
if(sum == ) return true;
else if(squareSum.find(sum)!=squareSum.end()) return false;
else {
squareSum.insert(sum);
return dfs(sum, squareSum);
}
}
};
202. Happy Number (INT)的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 变量存储缓存机制 Number (int bool float complex)
# ###变量存储的缓存机制(为了节省空间) #Number (int bool float complex) # (1) int -5~正无穷范围内 var1 = 18 var2 = 18 var1 ...
- Number (int float bool complex)--》int 整型、二进制整型、八进制整型、十六进制整型
# ### Number (int float bool complex) # (1) int 整型 (正整数 0 负整数) intvar = 15 print(intvar) intvar = 0 ...
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- UI5-学习篇-15-云连接SAP Cloud Connector
请关注地址:http://blog.itpub.net/29829936/viewspace-2128829/ 1.SCC下载及安装 https://tools.hana.ondemand.com/# ...
- hadoop-1
结合其他文章 http://weixiaolu.iteye.com/blog/1504898 https://www.cnblogs.com/dycg/p/3934394.html https://b ...
- Android app 性能优化的思考--性能卡顿不好的原因在哪?
说到 Android 系统手机,大部分人的印象是用了一段时间就变得有点卡顿,有些程序在运行期间莫名其妙的出现崩溃,打开系统文件夹一看,发现多了很多文件,然后用手机管家 APP 不断地进行清理优化 ,才 ...
- 更换Notepad++主题与字体(下载与配置)
本文推荐一款自用主题----tomorrow-theme!其github上有着绝大多数IDE和文本编辑器的这个主题的适配文件.可以在README中先预览一下你要的主题的具体名称.链接:https: ...
- Class-reference types 类引用类型--快要失传的技术
先摘一段原版的说明: A class-reference type, sometimes called a metaclass, is denoted by a construction of the ...
- python 多重继承 深度优先还是广度优先
我们常说,python2 是深度优先,python3 是广度优先, 其实具体来说是 python2.2 及其以前是深度优先 python2.3及其以后就是广度优先了 python官网 讲解1 以及su ...
- git-04 同意分支合并
https://blog.csdn.net/boysky0015/article/details/78185879/
- hadoop-1(单机模式配置)
下一次的目标是: 把Hadoop的伪分布式配置出来 1:ssh免密码登陆第一步 2:安装java环境 3:安装hadoop2
- http://sourceforge.net/projects/rtspdirectshow/
如何做一个解析rtsp协议的h264压缩的实时视频流播放器,带保存功能,目前我有rtsp协议的h264压缩后的实时视频流,目前想开发一个客户端,来播放该实时视频流,同时保存为视频文件,目前似乎有方案是 ...
- iphone上实现H264 硬编码
供ios下h264硬编码sdk,可以生成h264流. 我们的H264 SDK提供了一个理想的软件开发工具包,使您的app可以在iPhone或iPod上实时的访问H264帧数据.SDK提供了一套API功 ...