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. 什么是谷歌loon计划

    互联网服务已经与人类的生活密不可分,但受地理环境限制,目前全球只有三分之一的幸运儿能够体验到这种服务.为了让更多的人感受互联网,Google推出了一项名为“Project Loon”的计划,利用氢气球 ...

  2. Tomcat Server Configuration Automation Reinforcement

    目录 . 引言 . 黑客针对WEB Server会有那些攻击面 . 针对Tomcat Server可以做的安全加固 . Managing Security Realms with JMX . 实现对T ...

  3. workon在zsh中不起作用

    先装了workon,然后装了zsh,发现在zsh里不起作用 翻了一下网上没有解答,就看了看bashrc文件,发现一句 source /usr/local/bin/virtualenvwrapper.s ...

  4. servlet 中 web.xml

    <servlet> <servlet-mapping> 他们之间的关系可以使一对一,也可是一对多的关系. <servlet> <servlet-name> ...

  5. 依赖管理工具漫谈--从Maven,Gradle到Go

    http://jolestar.com/dependency-management-tools-maven-gradle/

  6. apt-get常用命令

    apt-get常用命令 一,什么的是apt-get 高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian及其衍生发行版(如:ubuntu)的软件包管理器. ...

  7. SCI完全攻略:从构思到发表

  8. 利用dedecms autoindex让文章列表加上序列号

    有些时候我们在制作模板的需要在文章标题前面加上序列号,可以通过织梦自带的autoindex属性来实现,实现方法很简单,只需要在序号递增的地方加上 这段代码就行,[field:global runphp ...

  9. 机器人与机器人仿真技术(zz)

    http://www.viblue.com/archives/5587.htm 一.机器人简介: 机器人(Robot)是自动执行工作的机器装置.它既可以接受人类指挥,又可以运行预先编排的程序,也可以根 ...

  10. SilverLight 条码 扫描枪 MVVM(转载)

    目前做SL项目时,遇到条码的问题. 客户需要通过条码来加快工作效率以及减少错误.有条码,自然便引入扫描枪这个东西.不解释. 关键点是:扫描枪每次扫描完毕会触发回车 这是解决问题的关键! 现有两种情景: ...