forked from Jensenczx/CodeEveryday
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_HappyNumber.java
More file actions
35 lines (33 loc) · 775 Bytes
/
4_HappyNumber.java
File metadata and controls
35 lines (33 loc) · 775 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class Solution {
/**
* @param n an integer
* @return true if this is a happy number or false
*/
public static boolean isHappy(int n) {
// Write your code here
if(n==0)
return false;
int[] array = new int[811];
for(int i=0;i<array.length; i++)
array[i] = 0;
while(getTheNum(n)!=1){
n = getTheNum(n);
if(array[n]==1)
return false;
array[n]=1;
}
return true;
}
public static int getTheNum(int n){
int num = 0;
while(n!=0){
int tmp = n%10;
num += tmp*tmp;
n = n/10;
}
return num;
}
public static void main(String[] args){
System.out.println(isHappy(19));
}
}