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
解题思路:

HashMap,JAVA实现如下:

    public boolean isHappy(int n) {
HashMap<Integer,Boolean> hm=new HashMap<Integer,Boolean>();
while(!hm.containsKey(n)&&n!=1){
hm.put(n, true);
n=HappyTime(n);
}
return n==1;
}
static public int HappyTime(int n){
int res=0;
while(n!=0){
res+=(n%10)*(n%10);
n/=10;
}
return res;
}

Java for LeetCode 202 Happy Number的更多相关文章

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

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

  2. Java [Leetcode 202]Happy Number

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

  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 快乐数

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

  5. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  6. LeetCode 202 Happy Number

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

  7. Java for LeetCode 065 Valid Number

    Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...

  8. (easy)LeetCode 202.Happy Number

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

  9. 40. leetcode 202. Happy Number

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

随机推荐

  1. BZOJ-1008 越狱 数论快速幂

    1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 6192 Solved: 2636 [Submit][Status] ...

  2. 洛谷P1993 小 K 的农场

    题目描述 小 K 在 Minecraft 里面建立很多很多的农场,总共 n 个,以至于他自己都忘记了每个 农场中种植作物的具体数量了,他只记得一些含糊的信息(共 m 个),以下列三种形式描 述: 农场 ...

  3. 全排列(java版)

    适用于不同数字的全排列,其实也适用于有重复数字的全排列,只不过的出来的结果有重复,需手动删减掉重复的组合. package testFullPermutation; import java.util. ...

  4. POJ1745Divisibility(01背包思想)

    Divisibility Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11151   Accepted: 3993 Des ...

  5. linux 破解版confluence安装

    OS  centos 6.5  需要的安装包如下: jre-7u67-linux-x64.rpm atlassian-confluence-5.4.4-x64.bin mysql-connector- ...

  6. 采用get的方式提交数据到服务器

    1  效果演示:

  7. 关于updateElement接口

    1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...

  8. 关于 jsonp 跨域

    jquery的jsonp真是一个很深沉的东西.....我一直以为,jsonp就是和json一样,然而并不是,jsonp的返回格式必须包在定义的回调里面...就是这样,默认是cb.要不然就报错,就是这样 ...

  9. POJ 1276 Cash Machine

    Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24213 Accepted: 8476 Descrip ...

  10. 如何将北京时间批量转为Unix时间?用Excel!

    前面我们说过Unix时间戳转换怎样在Excel批量修改,有些人就想如果有特殊需求,那能不能批量将北京时间批量转成unix时间呢?能!用Excel就可以实现!跟ytkah一起试试吧. 将unix时间戳转 ...