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 ...
随机推荐
- php封装pdo操作数据的工具类
<?php header("Content-Type:text/html;charset=utf-8"); class PdoMysql{ public static $co ...
- LeetCode: 598 Range Addition II(easy)
题目: Given an m * n matrix M initialized with all 0's and several update operations. Operations are r ...
- 计总与排名SUM和RANK函数
准备一些数据: CREATE TABLE [dbo].[SalesPerformance]( ,) NOT NULL, ) NOT NULL, [OrderDate] [DATE] NULL, ,) ...
- Scipy的应用
首先总体概括一下Scipy的用处 >>> #Scipy依赖于numpy>>> #Scipy提供了真正的矩阵>>> #Scipy包含的功能:最优化, ...
- 获得HttpWebResponse请求的详细错误内容
try { } catch (WebException ex) { HttpWebResponse response = (HttpWebResponse)ex.Response; Console.W ...
- 微调Inception V3网络-对Satellite分类
目录 1. 流程概述 2. 准备数据集 2.1 Satellite数据集介绍 3. Inception V3网络 4. 训练 4.1 基于Keras微调Inception V3网络 4.2 Keras ...
- hdu1754(线段树单点替换&区间最值模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...
- 计蒜课/ 微软大楼设计方案/中等(xjb)
题目链接:https://nanti.jisuanke.com/t/15772 题意:中文题诶- 思路:对于坐标为p1(x1, y1), p2(x2, y2) 的两个核心, 其中 x1 <= x ...
- 基于ZFAKA二次开发,添加PayJS支付渠道
项目地址:https://github.com/hiyouli/payjs-for-zfaka 关于ZFAKA,请移步:ZFAKA 免费.安全.稳定.高效的发卡系统,值得拥有! 演示地址:http:/ ...
- exportExcel()方法注意事项
1.保证数据集里的字段和SQL语句里字段全部一致,包括sql语句里必须有系统字段 2.exportExcel()执行的时候,是先去执行SQL语句,再去到数据集里面进行不对,若有不一致的地方,则报列名无 ...