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 ...
随机推荐
- 面试题: 数据库笔试 sql操作 已看 上课的练习题50sql
2018/5/31 oracle数据库面试笔试试题总结http://www.yjbys.com/qiuzhizhinan/show-308759.html 1/4Oracle数据库1.基础测试选择在部 ...
- RHEL6安装JDK7
一.安装准备 1.操作系统:redhat-server-6.1-x86_64 下载地址: http://www.verycd.com/files/d39b97540497d24175340915244 ...
- 4、map和Tuple
一.map 1.创建map //创建一个不可变的Map scala> val ages = Map("Leo" -> 30, "Jen" -> ...
- 面试总结hashmap
考点: 1.hashing的概念 2.HashMap中解决碰撞的方法 3.equals()和hashCode()的应用,以及它们在HashMap中的重要性 4.不可变对象的好处 5.HashMap多线 ...
- Finding Comments in Source Code Using Regular Expressions
Many text editors have advanced find (and replace) features. When I’m programming, I like to use an ...
- Linux之configure make make install
正常的编译安装/卸载: 源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). configure文件是一个可执行的脚本文件,它有很多选项, ...
- POJ2549【hash分离链接法】
题意: 给n个不同的数,求一个4个数(a,b,c,d)的组合满足a+b+c=d;求最大的d. 思路: 没想到可以用hash搞/ 这个就是数据结构里的分离链接法~ 解决hash冲突的方法:将所有关键字为 ...
- 洛谷P4213 【模板】杜教筛(Sum)(杜教筛,莫比乌斯反演)
传送门 坑着,联赛活着回来再填(死了就不填了) // luogu-judger-enable-o2 //minamoto #include<iostream> #include<cs ...
- SequoiaDB培训视频
很久之前录制的SequoiaDB培训视频,现在都放上百度云盘了,感兴趣的同学可以看看. 第一讲:图形界面-安装前准备 链接: https://pan.baidu.com/s/1d2B3qUYqtKrE ...
- Java进程占用内存过高,排查解决方法
最近收到邮件报警,说内存使作率达到84%.如下图: 解决方法: A:可能是代码原因导致的问题: 1.使用命令:top 查看当前进程的状态 2.从上图可以看到PID:916的java进程占用内存较大.定 ...