LeetCode Happy Number 开心数字
题意:
给出一个整数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 开心数字的更多相关文章
- [LeetCode] Valid Number 验证数字
Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => ...
- [LeetCode] Perfect Number 完美数字
We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- Python Number(数字)
---Number类型的细节,其包含的基本数字类型 ---Number基本数字类型之间的数值转换 ---Number上面的数学函数,有些可以直接调用,有些需要导入库 参见http://www.runo ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 力不从心 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 ...
随机推荐
- [hdu2063]过山车(二分图匹配)
题意:每个女人有感兴趣的k个男人,过山车两人一组,求最大匹配数. 解题关键:二分图最大匹配.匈牙利算法求解. 1.链式前向星建图 #include<cstdio> #include< ...
- TripAdvisor architecture 2011/06
http://highscalability.com/blog/2011/6/27/tripadvisor-architecture-40m-visitors-200m-dynamic-page-vi ...
- 条款20.宁以pass-by-reference-to-const替换pass-by-vlaue
缺省情况下c++以by value的方式传递对象至(或来自)函数.除非你另外指定,否则函数参数都是以实际实参的复件(副本)为初值,而调用端所获得的亦是函数返回值的一个复件.这些复件是由对象的c ...
- (十四)hibernate逆向工程
一.hibernate逆向工程生成实体 介绍一个模型设计工具PowerDesigner,这个是j2ee开发必要的一个工具.一般在开发中先使用PowerDesigner 创建实体关系图即概念模型.建立了 ...
- VMWare虚拟机Bridged类型网卡ping不通的原因和解决办法
要使VM与局域网内的其他机器一个子网,VM的网卡设置使用桥接.本来一直正常好好的, 突然有一天,遇到VMWare虚拟机Bridged类型网卡ping不通,设置,重启,查看VM网络设置,重装VMWare ...
- 火狐restclient安装和使用
RESTClient是一款用于测试各种Web服务的插件,它可以向服务器发送各种HTTP请求(用户也可以自定义请求方式),并显示服务器响应.使用RESTClient您可以方便的测试各种Web服务,为您的 ...
- 浅析Jupyter Notebook
一 概述 Jupyter Notebook是以web交互式的编程接口,是IPython notebook的升级版本.主要是针对python,另外支持运行 40 多种编程语言.Jupyter可以在个人机 ...
- (转)机器学习——深度学习(Deep Learning)
from:http://blog.csdn.net/abcjennifer/article/details/7826917 Deep Learning是机器学习中一个非常接近AI的领域,其动机在于建立 ...
- ZOJ2868【折半】
题意: 把一堆数分成两堆,使得两堆的差值最小. 思路: 先把一堆数分成两堆,然后用个set存一堆的所有组合,枚举第一堆的状态,二分查找第二堆接近half_value. 瞎说时间复杂度:O(2^17*3 ...
- 移动端专用css
通过设置css属性 -webkit-tap-highlight-color: rgba(0, 0, 0, 0);取消掉手机端webkit浏览器 点击按钮或超链接之类的 默认灰色背景色 设置css属性 ...