题意:

  给出一个整数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. Linux命令总结_查看主机磁盘使用

    1.dh -h 查看各个挂载点的使用量 2.du -sh *(星号表示当前所有文件夹)可以查看当前目录下各个文件夹的大小,-s表示只显示当前文件夹(不加-s你可以看到所有文件夹下的子文件夹的大小,太多 ...

  2. 性能测试之Jmeter学习(九)

    本节主要学习:定时器(部分内容引用http://www.cnblogs.com/yangxia-test) Meter也有像LR中的集合点,本节就来介绍下JMeter的集合点如何去实现. JMeter ...

  3. wxGlade的图标,原来是来自蒙德里安的名画!

    一直用wxGlade做GUI的,今天突然发现它的图标和一副油画很像. wxGlade的图标,图标的文件名竟然就叫做mondrian.ico 蒙德里安创造了很多这种纯粹的基本要素的作品,下面是其中之一, ...

  4. js中push(),pop(),unshift(),shift()的用法

    js中push(),pop(),unshift(),shift()的用法小结   1.push().pop()和unshift().shift() 这两组同为对数组的操作,并且会改变数组的本身的长度及 ...

  5. js基础(补10.10)

    1.内嵌式: <html> <head> <title></title> </head> <body> <a href=& ...

  6. 如何選擇最佳的 Wi-Fi 無線網路頻道,獲得最佳的傳輸速度(转载)

    转自:https://blog.gtwang.org/useful-tools/how-to-find-the-best-wi-fi-channel-for-your-router/

  7. hyperledger fabric 1.0.5 分布式部署 (八)

    gdb debug peer 程序 在开始我们从 github 上download 下来的源码包,实际上已经包含了可执行的 peer 程序,但是该程序是使用 release 方式编译的,并不支持gdb ...

  8. [Swift]LeetCode1078. Bigram 分词 | Occurrences After Bigram

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. Luogu P2257 YY的GCD 莫比乌斯反演

    第一道莫比乌斯反演...$qwq$ 设$f(d)=\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d]$ $F(n)=\sum_{n|d}f(d)=\lfloor \frac{N ...

  10. POJ3694 Network 边双缩点+LCA+并查集

    辣鸡错误:把dfs和ldfs搞混...QAQ 题意:给定一个无向图,然后查询q次,求每次查询就在图上增加一条边,求剩余割边的个数. 先把边双缩点,然后预处理出LCA的倍增数组: 然后加边时,从u往上跳 ...