题意:

  给出一个整数n,判断其是否为幸运数。

  规则是,将n按十进制逐位拆出来后,每个位各自进行取平方,再将这些平方数求和作为新的数字n。若最后n=1,就是幸运数。

思路:

  计算例子:n=47,接着n=4*4+7*7=65,接着n=6*6+5*5=61,接着....

  注意有可能陷入无限循环,就是迭代的过程产生了一个环路,即n又重复出现了。

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

Happy Number

python3

 class Solution(object):
def isHappy(self, n):
"""
:type n: int
:rtype: bool
"""
sett=set([n])
while n>1:
n=sum([int(x)**2 for x in str(n)])
if n in sett: return False
sett.add(n)
return True

AC代码

LeetCode Happy Number 开心数字的更多相关文章

  1. [LeetCode] Valid Number 验证数字

    Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...

  2. [LeetCode] Perfect Number 完美数字

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

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

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

  4. Python Number(数字)

    ---Number类型的细节,其包含的基本数字类型 ---Number基本数字类型之间的数值转换 ---Number上面的数学函数,有些可以直接调用,有些需要导入库 参见http://www.runo ...

  5. [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 ...

  6. [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 ...

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

  8. [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 ...

  9. 力不从心 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 ...

随机推荐

  1. petrozavodsk1

    A 转化模型和相当于求解小于n/2的最大的和n互质的数字, 显然可以证明所求和n/2相距 O(logn) ,从 n/2 开始向下枚举然后判定即可. B 上下界网络流? C 从底层开始向上走贪心选下层节 ...

  2. iView之select获取value和label

    使用:label-in-value="true" @on-change="obtainValue" 详见官方文档:https://www.iviewui.com ...

  3. 读取web应用下的资源文件(例如properties)

    package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...

  4. Ubuntu中的minicom

    需要更新一下软件源: sudo apt-get update 安装 在终端中输入sudo apt-get install minicom 配置 输入sudo minicom -s,注意前边一定要加su ...

  5. 2014年第五届蓝桥杯国赛试题(JavaA组)

    1.结果填空 (满分15分)2.结果填空 (满分45分)3.代码填空 (满分30分)4.程序设计(满分30分)5.程序设计(满分80分)6.程序设计(满分100分) 1.标题:海盗分金币 有5个海盗, ...

  6. Razor的主版页面框架

    类似于2.0版本中的MasterPage主版页面框架,不过mvc3.0推出的RazorView内建的主版页面语法与原本的webFormview的MasterPage相差甚远   1,Razor的页面执 ...

  7. Golang : cobra 包简介

    Cobra 是一个 Golang 包,它提供了简单的接口来创建命令行程序.同时,Cobra 也是一个应用程序,用来生成应用框架,从而开发以 Cobra 为基础的应用.本文的演示环境为 ubuntu 1 ...

  8. Python绘制正态分布曲线

      使用Python绘制正态分布曲线,借助matplotlib绘图工具: \[ f(x) = \dfrac{1}{\sqrt{2\pi}\sigma}\exp(-\dfrac{(x-\mu)^2}{2 ...

  9. 宽度设置百分比 高度跟宽度一样css解决方案

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 剑指Offer的学习笔记(C#篇)-- 栈的压入、弹出序列

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...