MCPcopy Create free account
hub / github.com/neetcode-gh/leetcode / isHappy

Method isHappy

java/0202-happy-number.java:3–20  ·  view source on GitHub ↗
(int n)

Source from the content-addressed store, hash-verified

1class Solution {
2
3 public boolean isHappy(int n) {
4 if (n == 1 || n == -1) {
5 return true;
6 }
7
8 Set<Integer> visit = new HashSet<Integer>();
9
10 // compute square until getting duplicate value
11 while (!visit.contains(n)) {
12 visit.add(n);
13 // using helper function to compute the sum of squares
14 n = sumOfSquare(n);
15
16 if (n == 1) return true;
17 }
18
19 return false;
20 }
21
22 public int sumOfSquare(int n) {
23 int output = 0;

Callers

nothing calls this directly

Calls 3

sumOfSquareMethod · 0.95
containsMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected