(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 ...
随机推荐
- 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例
根据08_android入门_android-async-http开源项目介绍及使用方法的介绍,我们通过最常见的登陆案例进行介绍android-async-http开源项目中有关类的使用.希望对你学习 ...
- java中的堆、栈、常量池以及String类型的两种声明
参考自http://blog.sina.com.cn/s/blog_798b04f90100ta67.html http://www.cnblogs.com/fguozhu/articles/2661 ...
- java读写文件大全
java读写文件大全 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int o ...
- BI案例:KPI在商业智能中的应用(ZT)
KPI(Key Performance Indication)即关键业绩指标,是通过对组织内部某一流程的输入端.输出端的关键参数进行设置.取样.计算.分析,衡量流程绩效的一种目标式量化管理指标,是把企 ...
- mootools upgrate from 1.2 to 1.3 or 1.4
Update from 1.2 to 1.3 lorenzos edited this page on 8 Jul 2011 · 2 revisions Pages 19 Home Home Chan ...
- LintCode "4 Sum"
4 Pointer solution. Key: when moving pointers, we skip duplicated ones. Ref: https://github.com/xbz/ ...
- IntelliJ IDEA通过Spring配置连接MySQL数据库
先从菜单View→Tool Windows→Database打开数据库工具窗口,如下图所示: 点击Database工具窗口左上角添加按钮"+",选择Import from sour ...
- bzoj4642: 泡泡
Description "OI真的像是一条奇趣横生的路啊,也许它是绕过了高考的大山,也许确实有通往大学的捷径.但我,真的,真的只在 乎那路上美丽的泡泡." --TB TB喜欢所 ...
- Python-While刷博爬虫
仅用于测试 #!/usr/bin/python import webbrowser as web import time import os url = 'www.abc.com' while Tru ...
- 如何实现一个malloc
任何一个用过或学过C的人对malloc都不会陌生.大家都知道malloc可以分配一段连续的内存空间,并且在不再使用时可以通过free释放掉.但是,许多程序员对malloc背后的事情并不熟悉,许多人甚至 ...