Skip to content

Commit

Permalink
LeetCode202.快乐数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxuechen committed Jun 29, 2018
1 parent aac7fd1 commit f64204e
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions LeetCode1/LeetCode202.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package leetCode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class LeetCode202 {

public static void main(String[] args) {
// TODO Auto-generated method stub
// int n=recount(1024);
// System.out.println(n);
//isHappy(108);
boolean result=happy(11);
if(result==true) {
System.out.println("true");
}else {
System.out.println("false");
}
}
public static boolean happy(int n) {
if(n<=0) {
return false;
}
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
return isHappy(n,map);
}
public static boolean isHappy(int n,Map<Integer,Integer> map) {
if(map.get(n)!=null) {
return false;
}
map.put(n, 1);
while(n!=0) {
n=recount(n);
if(n==1) {
return true;
}else {
return isHappy(n,map);
}

}
// System.out.println(n);
return false;
}

public static int recount(int n) {
ArrayList<Integer> list=new ArrayList<Integer>();
int temp=0,sum=0;
while(n>0) {
temp=n%10;
list.add(temp);
n/=10;
}
for(Integer i:list) {
temp=i*i;
sum+=temp;
// System.out.println(i);
}
return sum;
}

}

0 comments on commit f64204e

Please sign in to comment.