Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7440a75

Browse files
refactor 202
1 parent47cbcd5 commit7440a75

File tree

1 file changed

+18
-14
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-14
lines changed

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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* Those numbers for which this process ends in 1 are happy numbers.
1616
*
1717
* Example: 19 is a happy number
18-
*
1918
* 12 + 92 = 82
2019
* 82 + 22 = 68
2120
* 62 + 82 = 100
@@ -27,22 +26,27 @@ public boolean isHappy(int n) {
2726
if (n ==1) {
2827
returntrue;
2928
}
30-
Set<Integer>set =newHashSet();
31-
while (n !=1) {
32-
Stringstr =String.valueOf(n);
33-
n =0;
34-
for (inti =0;i <str.length();i++) {
35-
inttemp =Character.getNumericValue(str.charAt(i));
36-
n +=temp *temp;
37-
}
38-
if (n ==1) {
39-
returntrue;
40-
}
41-
if (!set.add(n)) {
29+
Set<Integer>seen =newHashSet();
30+
seen.add(n);
31+
intresult =0;
32+
while (result !=1) {
33+
result =getResult(n);
34+
if (seen.contains(result)) {
4235
returnfalse;
4336
}
37+
seen.add(result);
38+
n =result;
39+
}
40+
returntrue;
41+
}
42+
43+
privateIntegergetResult(intnum) {
44+
intresult =0;
45+
while (num !=0) {
46+
result += (num %10) * (num %10);
47+
num /=10;
4448
}
45-
returnfalse;
49+
returnresult;
4650
}
4751
}
4852
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp