-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
34 lines (33 loc) · 806 Bytes
/
Copy pathsolution.java
File metadata and controls
34 lines (33 loc) · 806 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
// 202. Happy Number
// https://leetcode.com/problems/happy-number/
// Easy | Java | Accepted 2026-01-21
// Runtime 4 ms | Memory 43.7 MB
class Solution {
public boolean isHappy(int n) {
String val = Integer.toString(n);
long sum = n;
if(n==1)
{
return true;
}
Map<Long, Integer> map = new HashMap<>();
while(true)
{
val = Long.toString(sum);
sum = 0;
for(int i = 0; i<val.length(); i++)
{
sum += Long.parseLong(val.substring(i,i+1))*Long.parseLong(val.substring(i,i+1));
}
if(sum==1)
{
return true;
}
if(map.containsKey(sum))
{
return false;
}
map.put(sum, 1);
}
}
}