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)的更多相关文章

  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. 变量存储缓存机制 Number (int bool float complex)

    # ###变量存储的缓存机制(为了节省空间) #Number (int bool float complex) # (1) int -5~正无穷范围内 var1 = 18 var2 = 18 var1 ...

  4. Number (int float bool complex)--》int 整型、二进制整型、八进制整型、十六进制整型

    # ### Number (int float bool complex) # (1) int 整型 (正整数 0 负整数) intvar = 15 print(intvar) intvar = 0 ...

  5. [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示

    有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...

  6. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  7. 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' ...

  8. Java for LeetCode 202 Happy Number

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

  9. (easy)LeetCode 202.Happy Number

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

随机推荐

  1. 让linux每天定时备份MySQL数据库并删除五天前的备份文件

    MYSQL定期备份是一项重要的工作,但人工操作太繁琐,也难避免有所疏漏,使用下面的方法即可让系统定期备份数据.利用系统crontab来定时执行备份文件,按日期对备份结果进行保存,达到备份的目的. 1. ...

  2. linux中根据进程的PID值来查找执行文件的及其路径

    lsof -p PID http://blog.csdn.net/great_smile/article/details/50114133

  3. ejs 用到的语法

    1.ejs 服务端渲染模板 2.语法: 01. <%= 变量名 %> -原样输出,不解析标签 02. <% js代码 %> 03. <%- 变量名%> -解析标签 ...

  4. java 线程状态相关测试

    1. 启动netty server 等待接受客户端连接 package io.netty.example.myTest.nio; import java.io.IOException; import ...

  5. sublime text3:sublime text3本地服务器方式运行文件

    网址:https://blog.csdn.net/md1688/article/details/70562381 1.Ctrl + Shift +P,启动Sublime Text的命令行(如果没有需要 ...

  6. 自己写一个spring boot starter

    https://blog.csdn.net/liuchuanhong1/article/details/55057135

  7. git release功能

    命令行: git tag -a v3. -m "这是4.0版本" git push origin v3. //git tag -a 标签名称 -m "说明" / ...

  8. How to Pronounce the Months of the Year

    How to Pronounce the Months of the Year Share Tweet Share Tagged With: Most Popular Some of the mont ...

  9. GBDT+Lr

    https://blog.csdn.net/shine19930820/article/details/71713680 http://scikit-learn.org/stable/auto_exa ...

  10. border&background1

    1.border-radius border-top-left-radius:10px; = border-top-left-radius:10px 10px; (水平10px 竖直10px 被正圆切 ...