Skip to content

Commit 8229a43

Browse files
refactor 202
1 parent 875d8d5 commit 8229a43

File tree

2 files changed

+58
-35
lines changed

2 files changed

+58
-35
lines changed

src/main/java/com/fishercoder/solutions/_202.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,47 @@
22

33
import java.util.HashSet;
44
import java.util.Set;
5-
/**Write an algorithm to determine if a number is "happy".
6-
A happy number is a number defined by the following process:
7-
Starting with any positive integer,
8-
replace the number by the sum of the squares of its digits,
9-
and repeat the process until the number equals 1 (where it will stay),
10-
or it loops endlessly in a cycle which does not include 1.
11-
Those numbers for which this process ends in 1 are happy numbers.
125

13-
Example: 19 is a happy number
14-
15-
12 + 92 = 82
16-
82 + 22 = 68
17-
62 + 82 = 100
18-
12 + 02 + 02 = 1*/
6+
/**
7+
* 202. Happy Number
8+
*
9+
* Write an algorithm to determine if a number is "happy".
10+
* A happy number is a number defined by the following process:
11+
* Starting with any positive integer,
12+
* replace the number by the sum of the squares of its digits,
13+
* and repeat the process until the number equals 1 (where it will stay),
14+
* or it loops endlessly in a cycle which does not include 1.
15+
* Those numbers for which this process ends in 1 are happy numbers.
16+
*
17+
* Example: 19 is a happy number
18+
*
19+
* 12 + 92 = 82
20+
* 82 + 22 = 68
21+
* 62 + 82 = 100
22+
* 12 + 02 + 02 = 1
23+
*/
1924
public class _202 {
20-
21-
public static boolean isHappy(int n) {
22-
if (n == 1) {
23-
return true;
24-
}
25-
Set<Integer> set = new HashSet();
26-
while (n != 1) {
27-
String str = String.valueOf(n);
28-
n = 0;
29-
for (int i = 0; i < str.length(); i++) {
30-
int temp = Character.getNumericValue(str.charAt(i));
31-
n += temp * temp;
32-
}
25+
public static class Solution1 {
26+
public boolean isHappy(int n) {
3327
if (n == 1) {
3428
return true;
3529
}
36-
if (!set.add(n)) {
37-
return false;
30+
Set<Integer> set = new HashSet();
31+
while (n != 1) {
32+
String str = String.valueOf(n);
33+
n = 0;
34+
for (int i = 0; i < str.length(); i++) {
35+
int temp = Character.getNumericValue(str.charAt(i));
36+
n += temp * temp;
37+
}
38+
if (n == 1) {
39+
return true;
40+
}
41+
if (!set.add(n)) {
42+
return false;
43+
}
3844
}
45+
return false;
3946
}
40-
return false;
41-
}
42-
43-
44-
public static void main(String... strings) {
45-
int n = 7;
46-
System.out.println(isHappy(n));
4747
}
4848
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._202;
4+
import com.fishercoder.solutions._53;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class _202Test {
12+
private static _202.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _202.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(true, solution1.isHappy(7));
22+
}
23+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy