(easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy".
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
- 12 + 92 = 82
- 82 + 22 = 68
- 62 + 82 = 100
- 12 + 02 + 02 = 1
Credits:
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
思路:使用HashSet判断重复。
代码如下:
public class Solution {
public boolean isHappy(int n) {
if(n<1) return false;
if(n==1) return true;
Set<Integer>set=new HashSet<Integer>();
set.add(n);
while(true){
int m=0;
int sum=0;
while(n!=0){
m=n%10;
n=n/10;
sum+=m*m;
}
if(sum==1) return true;
if(set.contains(sum)) return false;
set.add(sum);
n=sum;
}
}
}
运行结果:

(easy)LeetCode 202.Happy Number的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- LeetCode 202. Happy Number (快乐数字)
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- [LeetCode] 202. Happy Number 快乐数
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- Java for LeetCode 202 Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- (easy)LeetCode 263.Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 【easy】202. Happy Number
happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...
随机推荐
- ORA-01013:用户请求取消当前的操作
ORA-01013:用户请求取消当前的操作 在测试一个通过ODBC连接ORACLE数据库的VB程序时,总是出现该错误,估计应该是数据量比较大,导致超时. 查到解决方法有如下四种 (选任意一种即可): ...
- 【性能诊断】九、并发场景的性能分析(windbg案例,Fist Chance Exception/Crash dump)
经常会碰到这样的场景,自测及单单点的测试时没有任何问题,但在并发环境或生产环境下有时出现没规律的异常.报错等情况.在代码中增加日志是其中一种解决方式:抓取指定异常时的dump,通过wind ...
- Nginx出现413 Request Entity Too Large错误解决方法
Nginx出现的413 Request Entity Too Large错误,这个错误一般在上传文件的时候出现,打开nginx主配置文件nginx.conf,找到http{}段,添加 解决方法就是 打 ...
- eclipse项目里面的类有时候会莫名其妙出现很多错误
由于eclipse的编译是基于时间戳的判断机制的.因此当你按build all的时候有些eclipse认为时间戳没有改变的类不会被编译.因此你可以先clean一下再编译.这个时候eclipse会将 ...
- Angular学习(3)- 双向梆定
示例代码: <!DOCTYPE html> <html ng-app="MyApp"> <head> <title>Study 3& ...
- php base64编码和urlencode
base64编码 加密 base64_encode($str); 解密 base64_decode(base64_encode($str)); urlencode和base64混合使用 functio ...
- item2,实现singleton模式
单例模式? 只能实现一个实例的类成为单例. ============== muduo库中单例模式实现 #include<boost/noncopyable.hpp> //#include ...
- for语句的用法
#!/bin/bashfor i in 1 2 3 4 5 6do echo $idone 看文件 #!/bin/bashdir=$(ls /etc) for i in $dirdo echo $id ...
- 黄聪:利用Aspose.Word控件实现Word文档的操作(转)
撰写人:伍华聪 http://www.iqidi.com Aspose系列的控件,功能都挺好,之前一直在我的Winform开发框架中用Aspose.Cell来做报表输出,可以实现多样化的报表设计及 ...
- ADF_Database Develop系列1_通过UML数据库开发之建Logical UML Class Model
2013-05-01 Created By BaoXinjian