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的更多相关文章

  1. Leetcode 202 Happy Number 弗洛伊德判环解循环

    今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...

  2. LeetCode 202. Happy Number (快乐数字)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. [LeetCode] 202. Happy Number 快乐数

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. LeetCode 202 Happy Number

    Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...

  5. Java for LeetCode 202 Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  6. (easy)LeetCode 263.Ugly Number

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  7. Java [Leetcode 202]Happy Number

    题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  8. 40. leetcode 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  9. 【easy】202. Happy Number

    happy number Write an algorithm to determine if a number is "happy". A happy number is a n ...

随机推荐

  1. ImageView及其子类

    ImageView及其子类 (1)ImageView继承自View组件,任何Drawable对象都可以用ImageView来显示,ImageView派生出来了ImageButton.ZoomButto ...

  2. Reporting Service 报表报 rsReportServerDatabaseError 错的解决方法

    一.打开CRM的报表出现“报表服务器数据库内出错.此错误可能是因连接失败.超时或数据库中磁盘空间不足而导致的. (rsReportServerDatabaseError) 获取联机帮助 对象名 'Re ...

  3. Oracle Group by+rollup+cube 的应用

    首先我们创建一个示例表: Create table test_group (v_name varchar2(4) ,v_size varchar2(4) ,v_color varchar2(4) ,n ...

  4. 将war文件解压到指定目录

    问:如何将.war文件解压到指定目录? 答:jar命令没有这样的选项. eg:将abc.war解压到当前文件夹? 答:进入目标文件即abc.war文件所在的文件夹,按住shift键并在该文件夹空白处点 ...

  5. windows 下svn 创建分支 合并分支 冲突

    我用的系统是win7+Subversion 1.7.4.服务器搭建就略过了,我也是从网上找的,基本上就是几个命令吧!我用的CentOs6.5 .网上找了几个命令搭建很快,基本上是: 1.# sudo  ...

  6. openstack(liberty): devstack中的iniset/iniget函数分析

    这个ini开头的函数在devstack的启动配置中用的非常多,他主要负责.ini文件的配置,这个过程包括对相关ini文件的添加,注释,删除,获取信息,多行信息获取等. 这里主要说的iniset和ini ...

  7. UIview定义背景图片

    UIImage *image = [UIImage imageNamed:@"bgimagename"];    UIView *view = [[UIView alloc]ini ...

  8. (转)mongodb分片

    本文转载自:http://www.cnblogs.com/huangxincheng/archive/2012/03/07/2383284.html 在mongodb里面存在另一种集群,就是分片技术, ...

  9. bzoj1382: [Baltic2001]Mars Maps

    Description 给出N个矩形,N<=10000.其坐标不超过10^9.求其面积并 Input 先给出一个数字N,代表有N个矩形. 接下来N行,每行四个数,代表矩形的坐标. Output ...

  10. bzoj4716 假摔

    Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名 dalao.在游戏关卡的攻略中,可能 ...