File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 60
60
| 190 | [ Reverse Bits] ( https://leetcode.com/problems/reverse-bits ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/ReverseBits.java ) |
61
61
| 191 | [ Number of One Bits] ( https://leetcode.com/problems/number-of-1-bits ) | Easy | [ ![ Python] ( https://img.icons8.com/color/35/000000/python.png )] ( https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/python/number_of_1_bits.py ) |
62
62
| 198 | [ House Robber] ( https://leetcode.com/problems/house-robber ) | Easy | [ ![ Java] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/HouseRobber.java ) |
63
- | 202 | [ Happy Number] ( https://leetcode.com/problems/happy-number ) | Easy |
63
+ | 202 | [ Happy Number] ( https://leetcode.com/problems/happy-number ) | Easy | [ ![ Java ] ( https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png )] ( ) |
64
64
| 203 | [ Remove Linked List Elements] ( https://leetcode.com/problems/remove-linked-list-elements ) | Easy |
65
65
| 204 | [ Count Primes] ( https://leetcode.com/problems/count-primes ) | Easy |
66
66
| 205 | [ Isomorphic Strings] ( https://leetcode.com/problems/isomorphic-strings ) | Easy |
Original file line number Diff line number Diff line change
1
+ import java .util .HashSet ;
2
+ import java .util .Set ;
3
+
4
+ public class HappyNumber {
5
+ public static boolean isHappy (int number ) {
6
+ return isHappy (number , new HashSet <>());
7
+ }
8
+
9
+ private static boolean isHappy (long number , Set <Long > sum ) {
10
+ long sumSquaresDigits = sumDigitSquares (number );
11
+ if (sumSquaresDigits == 1 ) {
12
+ return true ;
13
+ }
14
+ if (sum .contains (sumSquaresDigits )) {
15
+ return false ;
16
+ }
17
+ sum .add (sumSquaresDigits );
18
+ return isHappy (sumSquaresDigits , sum );
19
+ }
20
+
21
+ private static long sumDigitSquares (long number ) {
22
+ long result = 0 ;
23
+ while (number > 0 ) {
24
+ result += Math .pow (number % 10 , 2 );
25
+ number /= 10 ;
26
+ }
27
+ return result ;
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments