Java for 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
解题思路:
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的更多相关文章
- Leetcode 202 Happy Number 弗洛伊德判环解循环
今天先谈下弗洛伊德判环,弗洛伊德判环原来是在一个圈内有两人跑步,同时起跑,一人的速度是另一人的两倍,则那个人能在下一圈追上另一个人,弗洛伊德判环能解数字会循环出现的题,比如说判断一个链表是不是循环链表 ...
- Java [Leetcode 202]Happy Number
题目描述: Write an algorithm to determine if a number is "happy". A happy number is a number d ...
- 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 ...
- Java for LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- LeetCode 202 Happy Number
Problem: Write an algorithm to determine if a number is "happy". A happy number is a numbe ...
- Java for LeetCode 065 Valid Number
Validate if a given string is numeric. Some examples: "0" => true " 0.1 " =&g ...
- (easy)LeetCode 202.Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- 40. leetcode 202. Happy Number
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
随机推荐
- Lucene -- 实时索引
lucene的实时搜索可以分成:实时和近实时的搜索. 实时只能依靠内存了. 近实时可以用lucene中提供org.apache.lucene.index.DirectoryReader.open(In ...
- 图解Android - 如何看Android的UML 图?
如何看UML图? UML能给我们带来什么? 这是本文要寻找的答案.UML图有很多类型,我们这里只讨论最重要也最常用的两种 - 类图和时序图. 1. 类图 通过类图,我们可以很容易的了解代码架构,理清模 ...
- 在java类中,是先执行类的构造函数还是先执行类的私有非静态变量
举例子: package test_instance; public class TestClassLoaderTime { public TestClassLoaderTime(){ System. ...
- Oracle自定义函数
核心提示:函数用于返回特定数据.执行时得找一个变量接收函数的返回值; 语法如下: create or replace function function_name ( argu1 [mode1] da ...
- BZOJ-1407 Savage 枚举+拓展欧几里得(+中国剩余定理??)
zky学长实力ACM赛制测试,和 大新闻(YveH) 和 华莱士(hjxcpg) 组队...2h 10T,开始 分工我搞A,大新闻B,华莱士C,于是开搞: 然而第一题巨鬼畜,想了40min发现似乎不可 ...
- NOI题库
07:机器翻译 总时间限制: 1000ms 内存限制: 65536kB 描述 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 这个翻译软件的原理很简单,它只是从头到尾,依次将每个 ...
- BZOJ4590 自动刷题机
Description 曾经发明了信号增幅仪的发明家SHTSC又公开了他的新发明:自动刷题机--一种可以自动AC题目的神秘装置.自动 刷题机刷题的方式非常简单:首先会瞬间得出题目的正确做法,然后开始写 ...
- javascript自动转换大小写
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...
- Jquery插件easyUi表单验证提交
<form id="myForm" method="post"> <table align="center" style= ...
- abstract 类也可以继承 实体类
public class BaseReq { public String UserId { get; set; } public BaseReq() { } } public abstract cla ...