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. python3倒叙字符串

    google测试工程师的一道题: 设计一个函数,使用任意语言,完成以下功能: 一个句子,将句子中的单词全部倒排过来,但单词的字母顺序不变.比如,This is a real world,输出结果为 w ...

  2. 【solr】 solr 5.4.1 和tomcat 基础环境搭建

    下载省略; solr下载地址:http://www.apache.org/dyn/closer.cgi/lucene/solr/ tomcat 下载安装(省略). solr5.4.1 默认在jetty ...

  3. 关于用phonegap+jquery moblie开发 白屏闪屏的解决方法

    前几天自己玩开发android应用,做些页面切换效果时,发现两个页面间切换间有白色闪屏的问题. 在网上找了很久的资料,还是没有解决. 最终,发现同事开发的android应用没有这个问题.对比代码排除发 ...

  4. JAVA解析各种编码密钥对(DER、PEM、openssh公钥)

    一.DER编码密钥对 先说下DER编码,是因为JCE本身是支持DER编码密钥对的解析的,可以参见PKCS8EncodedKeySpec和X509EncodedKeySpec. DER编码是ASN.1编 ...

  5. 在DataList、Repeater的HeaderTemplate和FooterTemplate模板中寻找控件FindControl

    [程序代码] <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> ...

  6. TCP程序设计

        在Java中使用Socket(套接字)完成TCP程序的开发,使用此类可以方便地建立可靠的.双向的.持续的.点对点的通信连接.     在Socket的程序开发中,服务器端使用ServerSoc ...

  7. kafka模拟客户端发送、接受消息

    producer   消息的生成者,即发布消息 consumer   消息的消费者,即订阅消息 broker     Kafka以集群的方式运行,可以由一个或多个服务组成,服务即broker zook ...

  8. SparkSQL相关语句总结

    1.in 不支持子查询 eg. select * from src where key in(select key from test); 支持查询个数 eg. select * from src w ...

  9. HDU 2087 剪花布条(KMP,不可重叠重复子串)

    给KMP传的数组一定要从0开始!! 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 这题和我上一篇博客类似,只不过不可重叠,我看了数据范围不大,所以就开 ...

  10. 《挑战程序设计竞赛》 4.1.1 矩阵 P286

    想写几篇挑战的感悟,也有助于自己理解这本书.但这上面大多贴的是书上的代码,主要是为了用的时候后直接复制就好了,这样就很方便了,就相当于黑盒模板了. 1.线性方程组 /** \brief 高斯消元法 * ...