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 ...
随机推荐
- Ubuntu下无法使用Secure_CRT连接服务器
虚拟机使用 1 .指令安装了SSH服务器 sudo apt-get install openssh-server 2. 输入命令 ps | grep ssh 查看SSH服务是否开启 显示服务已开启 3 ...
- 【转】简明 Vim 练级攻略
原地址:https://coolshell.cn/articles/5426.html vim的学习曲线相当的大(参看各种文本编辑器的学习曲线),所以,如果你一开始看到的是一大堆VIM的命令分类,你一 ...
- linux 显示系统执行的进程
ps -a 显示所有的进程信息 -u 以用户的形式显示系统进程 -x 显示后台进程运行的参数 netstat -anp |more 查看端口 查看开放的端口 vim/etc/sysconfig/ ...
- WMI Provider Host
WMI 即 Windows Management Instrumentation(Windows 管理规范)的简写,是 Windows 操作系统的一项内置功能,它为软件和管理脚本提供了一种标准化方法, ...
- day03-数据类型
数据类型 一.介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 mysql常用数据类型概括:#1. 数字: 整型:tinyint.int.bi ...
- 使用ssh密钥登录虚拟主机里的另一台主机报警的解决方案
提示:Address 192.168.*.* maps to localhost, but this does not map back to the address - POSSIBLE BREAK ...
- pandas 笔记
删除: del df["A"] # 原地修改 df.drop("a") # 返回修改后的新对象 df.drop(["a", " ...
- 关于AB包的释放与 Resources.UnloadUnusedAssets的关系
Resources.UnloadUnusedAssets 并不能释放AB包中东西,只能释放从AB包中加载出来的资源,也可以释放场景中的资源,其它不是从AB包加载来的资源. AB加载后,整个包都加载到内 ...
- Centos7升级新内核
由于觉得Centos7内核版本还不够高,就想升级下,下面是升级步骤 我使用的方法是使用yum升级内核 使用第三方仓库升级 CentOS 允许使用 ELRepo,这是一个第三方仓库,可以将内核升级到最新 ...
- RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World(转载)
RabbitMQ系列教程之一:我们从最简单的事情开始!Hello World 一.简介 RabbitMQ是一个消息的代理器,用于接收和发送消息,你可以这样想,他就是一个邮局,当您把需要寄送的邮件投递到 ...