-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path202.java
More file actions
23 lines (21 loc) · 709 Bytes
/
202.java
File metadata and controls
23 lines (21 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.ArrayList;
class Solution {
public boolean isHappy(int n) {
String number = Integer.toString(n);
ArrayList<Integer> passed = new ArrayList<Integer>();
while (true) {
int total = 0;
for (int i = 0; i < number.length(); i++) {
total += Math.pow(Character.getNumericValue(number.charAt(i)), 2);
}
passed.add(Integer.valueOf(number));
if (total == 1)
return true;
for (int i = 0; i < passed.size(); i++) {
if (total == passed.get(i))
return false;
}
number = Integer.toString(total);
}
}
}